Reputation: 2015
How to get data after - in sql server
current i have word something like 059 - Gaffey St.
So my result should be Gaffey St.
Upvotes: 0
Views: 70
Reputation: 69524
Try something like....
Declare @String VARCHAR(50) = '059 - Gaffey St.'
Select SUBSTRING(@String , CHARINDEX('-' , @String) + 1, LEN(@String))
Edit
To only get 59
without the leading zeros
Select CAST( LEFT(@String , CHARINDEX('-' , @String) - 1 ) AS INT)
Upvotes: 2
Reputation: 708
Here is it - letter part and number part:
DECLARE @String VARCHAR(50) = '059 - Gaffey Stop.'
SELECT SUBSTRING(@String , CHARINDEX('-' , @String) + 1, LEN(@String)) AS LetterPart,
CAST(LEFT(@String, PATINDEX('%[^0-9]%', @String+'.') - 1) AS INT) AS NumPart
Upvotes: 1
Reputation: 310
You should be able to use something like:
substring('059 - Gaffey St.', charindex('-', '059 - Gaffey St.') + 1, len('059 - Gaffey St.'))
You might wish to surround it by ltrim(rtrim(...)) to remove the eventual spaces.
Upvotes: 0