Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get numerical value between specified characters

Tags:

sql

sql-server

I have column having value like 'aAb314282069480098c1234d#20e000'.
I would like to get only '314282069480098' from this. How can I achieve this.

I have tried like this:

SUBSTRING(Mercury_AcqRefData,PATINDEX('%[0-9]%',Mercury_AcqRefData),LEN(Mercury_AcqRefData))

but I didn't achieve. Thanks in advance for your help.

like image 906
Dev Avatar asked Jan 02 '23 02:01

Dev


2 Answers

Try below it is giving required results:

DECLARE @string varchar(200) = 'aAb314282069480098c1234d#20e000'
select left(s, patindex('%[^0-9]%', s) - 1)
FROM (SELECT SUBSTRING(@string, patindex('%[0-9]%', @string), len(@string)) 
as S
 ) XX;
like image 59
UsmanMirza Avatar answered Jan 03 '23 15:01

UsmanMirza


Can you please try the following query:

DECLARE @Mercury_AcqRefData AS VARCHAR(200) = 'aAb31428206948220098c1234d#20e000';

DECLARE @Filter1 AS VARCHAR (200) = '';
SELECT @Filter1 = SUBSTRING(@Mercury_AcqRefData, PATINDEX('%[0-9]%', @Mercury_AcqRefData), LEN(@Mercury_AcqRefData));
SELECT SUBSTRING(@Filter1, 0, PATINDEX('%[A-Za-z]%', @Filter1));
like image 39
Arulkumar Avatar answered Jan 03 '23 15:01

Arulkumar