开发者

Display part of entry

开发者 https://www.devze.com 2022-12-22 22:16 出处:网络
Suppose a query \"select streetAdr from Address\" returns \"236 a1 road\" \"333 a2 road\" and 444 a4 road\" as 3 rows. How i can display only \"236\" \"333\"and开发者_StackOverflow中文版 \"444\" in SQ

Suppose a query "select streetAdr from Address" returns "236 a1 road" "333 a2 road" and 444 a4 road" as 3 rows. How i can display only "236" "333" and开发者_StackOverflow中文版 "444" in SQL Server.


Try:

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


Just to be on the safe side, if any of your addresses should only have a number and nothing else:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消