Use this space to put some text. Update this text in HTML

468x60 banner ad

Advertise with Us

Powered by Blogger.

Thursday, 25 February 2016

converting varchar value to integer decimal value in sql server


In sql server converting the value of records in a table with data type varchar to integer or decimal so you can get the sum of the value.

create table tm_Salary
(
  ID int,
  Salary_Amount varchar(50)
)

insert into tm_Salary values(1, '32.43');
insert into tm_Salary values(2, '43.33');
insert into tm_Salary values(3, '23.22');

sql Query:


SELECT  
   SUM(cast(Salary_Amount as decimal(4,2))) as Sum_Of_Salary  
FROM tm_Salary

OR

SELECT  
   SUM(cast(isnull(Salary_Amount,0) as decimal(12,2))) as Sum_Of_Salary 
FROM tm_Salary

OR

SELECT  
 SUM(convert(decimal(10,2), Salary_Amount)) as Sum_Of_Salary 
FROM tm_Salary

OR

SELECT 
  SUM(CAST(Salary_Amount AS decimal(6,2))) as Sum_Of_Salary  
FROM tm_Salary


 Result:




No comments :

Post a Comment

Ask a Question?