How to Get the first numeric value from the alphanumeric string in sql server

Solution:
--Get the first numeric value from the alphanumeric string
SELECT
Naration, SUBSTRING(Naration,
PATINDEX('%[0-9]%', Naration),
(CASE WHEN PATINDEX('%[^0-9]%', STUFF(Naration, 1, (PATINDEX('%[0-9]%', Naration) - 1), '')) = 0
THEN LEN(Naration) ELSE (PATINDEX('%[^0-9]%', STUFF(Naration, 1, (PATINDEX('%[0-9]%', Naration) - 1), ''))) - 1
END )
) AS Result
FROM dbo.TransactionTable
Result:

Post a Comment

0 Comments