I have a column which is defined as varchar. Is there any function available in sql server for having only the numeric values for the column and remove any other characters from it.
ex: 123abc -- 123
ab12c --12
ab45c? --45
I have a column which is defined as varchar. Is there any function available in sql server for having only the numeric values for the column and remove any other characters from it.
ex: 123abc -- 123
ab12c --12
ab45c? --45
You can try this:
DECLARE @temp TABLE
(
string NVARCHAR(50)
)
INSERT INTO @temp (string)
VALUES
('123abc'),
('ab12c'),
('ab45c?')
SELECT SUBSTRING(string, PATINDEX('%[0-9]%', string), PATINDEX('%[0-9][^0-9]%', string + 't') - PATINDEX('%[0-9]%',
string) + 1) AS Number
FROM @temp