user1030181
user1030181

Reputation: 2015

How to get data after '-' in sql server

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

Answers (3)

M.Ali
M.Ali

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

Amel
Amel

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

Dimitar Kyuchukov
Dimitar Kyuchukov

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

Related Questions