Click here to Skip to main content
15,887,304 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have 2 columns in table are

column name datatype is varchar
column Amount is nvarchar

Name Amount
Karthikeyan R 11
Karthikeyan R 51
Mohammedhussain T M 19
Mythili V 37
Rangaraj C 145- $4358.14

i want sum the amount against name.plz help me

What I have tried:

and i need resut below

C#
Name	Amount
Karthikeyan R	62
Mohammedhussain T M	19
Mythili V	37
Rangaraj C	145- $4358.14
Posted
Updated 17-Dec-16 0:38am

No can do. How do you expect to sum 145- $4358.14? In the first place, why is amount of type nvarchar? It defies common sense. Every field in a database table must have the correct data type that befits the purpose, interpretation, and usage of its data.
Change it to the correct data type that it is meant to be by referring to SQL General Data Types[^], otherwise you will encounter absurd situation such as this that results in awkward SQL manipulation which is very inefficient and unnecessary.
If the data type is correct, you can get what you want easily:
SQL
SELECT name, SUM(amount) FROM tablename GROUP BY name
 
Share this answer
 
v2
you can cast the column

C#
sum(CASE WHEN ISNUMERIC(Amount)=1 THEN CAST(CAST(amount AS float) AS INT)END ) 



Refer this -
SQL Query to sum nvarchar value[^]
 
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