I have a string that I need to break apart for codes.
String examples - 'H345678_30', 'q789038_155'
To extract the code after the underscore I have something that works
Select Code = RIGHT(@temp ,CHARINDEX('_',REVERSE(@temp)) - 1)
The code I'm having a hard time extracting is from the first part before the '_'. Lets say we take 'H345678_30', the first 3 characters are always Precodes like H34 or in the other example as mentioned aboved it will be Q78. The #s that follow after H34 and end before '_' is what I need.
In this case it will be 5678.
So far I have this but any suggestions to make this work more efficiently it will be great
Select TCode = LEFT((SUBSTRING(@temp,Charindex('q',@temp)+3,Len(@temp))), (charindex('_', ((SUBSTRING(@temp,Charindex('q',@temp)+3,Len(@temp))))))-1)
Thank you
如果前三个字符始终是“预编码”,则可以简单地执行以下操作:
SELECT SUBSTRING('H345678_30',4,CHARINDEX('_','H345678_30')-4)
You can try following code, I have captured everything that is on left side of '_' character and then I have removed first 3 characters to get 5678:
DECLARE @x VARCHAR(max) = 'H345678_30'
SELECT SUBSTRING(LEFT(@x, CHARINDEX('_', @x)-1), 4, 10)
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.