显示条目的一部分

时间:2010-03-12 10:37:17

标签: sql sql-server sql-server-2005

假设查询“从地址中选择streetAdr”返回“236 a1 road”“333 a2 road”和444 a4 road“as 3 rows。我怎么只能在SQL Server中显示”236“”333“和”444“

2 个答案:

答案 0 :(得分:2)

尝试:

Select left(yourcolumn, charindex(' ',yourcolumn)) ...

答案 1 :(得分:0)

为了安全起见,如果您的任何地址只有一个号码而没有别的:

declare @Address table (AddressLine1 nvarchar(50) NOT NULL)
insert into @Address values ('236 a1 road')
insert into @Address values ('333 a2 road')
insert into @Address values ('444 a4 road')
insert into @Address values ('555')
select
   CASE
      WHEN charindex(' ', AddressLine1) > 0 THEN
         Left(AddressLine1, charindex(' ', AddressLine1))
      ELSE
         AddressLine1
   END AS AddressLine1
from @Address