1

I have a table A like this:

ID   Col1
----------------------
1    xyz-abcccc
2    xyz-jkasdasd
3    abcds-asks
4    asdasdasda-as

I want to get output like this:

    ID   Col1
    -------------
    1    abcccc
    2    jkasdasd
    3    asks
    4    as

I want get output where anything before the dash - is ignored.

Thanks

2 Answers 2

2

charindex() would be a good place to start. The only trick is add a dash within the charindex function as a fail-safe thus avoids throwing an error.

Example

Select ID
      ,Col1 = substring(Col1,charindex('-',col1+'-')+1,len(Col1))
 from YourTable

Returns

ID  Col1
1   abcccc
2   jkasdasd
3   asks
4   as
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it worked. What should I do if I have more than 2 " - "? –
0

You can use a combination of RIGHT and CHARINDEX functions as well.

Query

select [ID],
case when [Col1] like '%-%' then right([Col1], charindex('-', reverse([Col1]), 1) - 1)
else [Col1] end as [new_col1]
from [your_table_name];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.