简体   繁体   中英

Extract only numeric values from String column

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

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM