Click here to Skip to main content
13,138,791 members (54,286 online)
Rate this:
 
Please Sign up or sign in to 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

Name	Amount
Karthikeyan R	62
Mohammedhussain T M	19
Mythili V	37
Rangaraj C	145- $4358.14
Posted 16-Dec-16 23:40pm
Updated 17-Dec-16 0:38am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:
SELECT name, SUM(amount) FROM tablename GROUP BY name
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

you can cast the column

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



Refer this -
SQL Query to sum nvarchar value[^]
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 17 Dec 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100