sql server - SQL - Get numbers from string after a pound (#) sign -


i attempting numbers string in sql server 2012 located after pound (#) sign , before spaces follow. instance, store numbers. let's have following:

big box store #450 big box store #768 little shop #2 widgets warehouse #678 little shop #5 widgets warehouse #559 corner boutiques #32 *closed corner boutiques #67 *closed corner boutiques #12 buy more #1047 superstore 1 stop shop #3 1 stop shop #17 2 me #16 

i return following: 450, 768, 2, 678, 5, 559, 32, 67, 12, 1047, 3, 17, 16.

as can see, not of strings have numbers @ end. of them have numerical character in name of store. figure best way of going extract numbers following pound sign.

is there way this? i've looked @ following articles:

query numbers string

https://www.sqlservercentral.com/forums/topic456023-338-1.aspx

it seems patindex may use, unsure i've tried far doesn't return expected results.

many thanks!

another similar way... using test data tyron. works if there isn't space after digits.

declare @t table(   mystring nvarchar(1000) );  insert @t values   ('big box store #450') ,('big box store #768') ,('little shop #2') ,('widgets warehouse #678') ,('little shop #5') ,('widgets warehouse #559') ,('corner boutiques #32*closed')    --notice no space here ,('corner boutiques #67 *closed') ,('corner boutiques #12') ,('buy more #1047 superstore') ,('1 stop shop #3') ,('1 stop shop #17') ,('you 2 me #16');  select     substring(mystring,charindex('#',mystring,0) + 1,case when patindex('%[^0-9]%',right(mystring,len(mystring) - charindex('#',mystring,0))) = 0 99 else patindex('%[^0-9]%',right(mystring,len(mystring) - charindex('#',mystring,0))) - 1 end)     --char version...    ,substring(mystring,charindex('#',mystring,0) + 1,case when patindex('%[^0-9]%',substring(mystring,charindex('#',mystring,0) + 1,len(mystring) - charindex('#',mystring,0) + 1)) = 0 99 else patindex('%[^0-9]%',substring(mystring,charindex('#',mystring,0) + 1,len(mystring) - charindex('#',mystring,0) + 1)) - 1 end)      @t 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -