Click here to Skip to main content
16,021,211 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
There is One Table Sal.there is only two columns.
create table sal(emp varhar(50), salary nvarchar(100))
In Salary Field there is so many values.How can i get Total amount of Salary column.
like
sal
10,000
20,000
30,000
how to get sum of sal values.
Posted
Comments
[no name] 14-Jul-14 7:29am    
Well since you foolishly decided to store your numbers as strings, you will have to convert the strings to numbers before you can sum them.

First of all, change the data type[^] for Salary column to accept numeric values instead of string. Use ALTER TABLE[^] comand:
SQL
ALTER TABLE SAL ALTER COLUMN Salary DECIMAL(10,2)

Secondly, use query like this:
SQL
SELECT SUM(Salary)
FROM SAL;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Jul-14 11:44am    
5ed.
—SA
Maciej Los 14-Jul-14 14:47pm    
Thank you, Sergey ;)
SQL
select SUM(CONVERT(money, salary)) AS Total from sal
 
Share this answer
 
Comments
Maciej Los 14-Jul-14 9:56am    
Good, but it needs some extra explanation. Have a look at my answer.
Hi benzimen,

select sum(cast(replace(salary,',','')as float)) from sal;

Hope this will helpful for you..
 
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