0

I have a column of values that are as shown below:

ID
x-644478134
x-439220334
x-645948923
x-10686843432
x-4273883234

I would like to return a column like so:

ID
644478134
439220334
645948923
10686843432
4273883234

Can someone advise how to do this cleanly? I believe it is something to do with substring but not sure exactly

SELECT SUBSTRING(d.ID)
FROM data d
2
  • Always starts with "x-" ??? If so, try: Select substring(ID,3,LEN(ID)-2) Commented Jul 22, 2021 at 20:09
  • Is this your answer? stackoverflow.com/questions/16667251/… Commented Jul 22, 2021 at 20:12

1 Answer 1

1

You need to use substring, charindex and len functions such as below, assuming the dash (-) is the separator of the text and numeric part:

declare @x varchar(50) = 'a-0123'
select substring(@x,CHARINDEX('-',@x,1)+1,len(@x)-CHARINDEX('-',@x,1))

If you are sure that ID always starts with x-, then:

Select substring(ID,3,LEN(ID)-2) 
Sign up to request clarification or add additional context in comments.

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.