Edinei Raduvanski
Edinei Raduvanski

Reputation: 55

How to return only records after certain character?

How to return only records after certain character? Example:

'1-1080599'
'021-1080599'
'02 -1080599 '

Expected outcome:

'1080599'

Upvotes: 1

Views: 43

Answers (2)

Richard Hansell
Richard Hansell

Reputation: 5403

vkp's solution will work fine, but you could also use PARSENAME to make things slightly simpler, e.g.:

SELECT PARSENAME(REPLACE('101-2345678', '-', '.'), 1);

If you want to get rid of spaces then you could do this:

SELECT LTRIM(PARSENAME(REPLACE('101 - 2345678', '-', '.'), 1));

...or even:

SELECT PARSENAME(REPLACE(REPLACE('101 - 2345678', '-', '.'), ' ', ''), 1);

Upvotes: 2

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49270

This works if you are looking to extract every character after - and if there is only one - character in the string.

select substring(column1,charindex('-',column1)+1,len(column1)) from tablename
where charindex('-',column1) > 0

or RIGHT can be used.

select right(column1,charindex('-',column1)) from tablename
where charindex('-',column1) > 0

Upvotes: 5

Related Questions