Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a varchar(100) column in a table that contains a mix of integers (as strings) and non-integer strings.

E.g.


2 Plates
3 Plates
10 Plates

I need to sum these integer values and the result become like this.

"15 Plates"

Is there any MS SQl query to show like that.


Please Help Me.


Thanks

Dileep....
Posted
Updated 16-Sep-12 20:39pm
v2
Comments
Andrei Straut 17-Sep-12 2:57am    
You can try and take a look at regular expressions (even though MSSQL has very basic support for them, you can define your own). Using regexes, you could return only the numeric part of the VARCHAR column, and then use another query or subquery to do the the SUM of the returned numeric parts
dilzz 17-Sep-12 3:20am    
can u please gimme a example for that.

Thanks

Dileep

Wrong design.
You need two columns:
- a column for the amount
- a column for the unit
Then you can do a
SQL
SELECT SUM(amount) as total, unit
FROM MYTABLE
GROUP BY unit
 
Share this answer
 
2 Plates
3 Plates
10 Plates

in above all string "plates" string is common.i hope following sql query works for you
SQL
select convert(varchar(max),convert(int,ltrim(rtrim(left('2 Plates',len('3 Plates')-len('Plates')))))+convert(int,ltrim(rtrim(left('3 Plates',len('3 Plates')-len('Plates')))))+convert(int,ltrim(rtrim(left('10 Plates',len('10 Plates')-len('Plates'))))))+right('10 Plates',len('Plates'))

if it will not help you inform i will give other soln.
make query dynamic as per your requirement.all the best.
 
Share this answer
 
Comments
dilzz 17-Sep-12 3:14am    
Sir, Sum times string may b change..... some time it become.
"Glass"

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