Wednesday, 29 November 2017

How to extract a string in between the characters from the string SQL?

Please check with this code, I was used in my project for extracting numeric value
Declare @str varchar(50)= 'userid7f(203001)31f469-NAIsasdsd'
select substring(@str,patindex('%(%',@str)+1,patindex('%)%',@str)-patindex('%(%',@str)-1)
OR
select substring(@str,charindex('(',@str)+1,charindex(')',@str)-charindex('(',@str)-1)
OUTPUT= '203001'

How to get the only numeric part from a string SQL?

How to get the only numeric part from a string SQL?
Please check with this code, I was used in my project for extracting numeric value
DECLARE @string varchar(100)
SET @string = 'sk123;fja4567afj;as8901sfsfsfa2'
WHILE PATINDEX('%[^0-9]%',@string) <> 0
SET @string = STUFF(@string,PATINDEX('%[^0-9]%',@string),1,'')
SELECT @string
OUTPUT= '123456789012'