Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one table containing few rows like

column
abc1
23abc
4nm

I want output like this

column
1
23
4

only numerical values

What I have tried:

select isnumeric(string)

but it is not going to help me out..

please help me on this
Posted
Updated 26-Apr-16 22:03pm
Comments
Sergey Alexandrovich Kryukov 27-Apr-16 3:45am    
This question is just the indication of wrong database schema design. Do you have an option to redesign it?
You need to keep numerical data in separate columns(s) and never use strings to represent numeric data.
—SA

It's a poor idea, because it's going to take significant server resources each time you do it: If you have a large number of rows (or do this frequently) you are going to waste a lot of Server processing time!
I'd recommend that either you allocate a second column which holds only the numeric values and insert that when you insert the other column, or do this in your presentation language where a simple regex will do it and distribute the load across different machines.

But...you can do it: tsql - T-SQL select query to remove non-numeric characters - Stack Overflow[^] - see the SQL function given by eDriven_Levar at the bottom.
I really don't recommend it though.
 
Share this answer
 
For example if your table name is tbltest and column name is columntest, your query will be like following,

SELECT LEFT(subsrt, PATINDEX('%[^0-9]%', subsrt + 't') - 1)
FROM (
SELECT subsrt = SUBSTRING(columntest, pos, LEN(columntest))
FROM (
SELECT columntest, pos = PATINDEX('%[0-9]%', columntest)
FROM tbltest
) d
) t
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900