Pat Doyle
Pat Doyle

Reputation: 384

Summing a Nvarchar in SQL

I am a rookie in the SQL world.

Anyway - I am trying to sum a column that is currently stored as a Nvarchar. I have done some research and it ended up that my query looked something like this:

SELECT SUM(CAST(Nvarchar_Col AS INTEGER)
FROM Datatable

I have no idea why but I keep getting the error: Incorrect syntax near the keyword 'FROM'

Any help would be greatly appreciated.

Upvotes: 1

Views: 3728

Answers (2)

sanjeev kumar
sanjeev kumar

Reputation: 11

ISNULL(SUM(CAST(paidamount AS int)),0) as paidamount

Upvotes: 0

S3S
S3S

Reputation: 25152

You can't CAST #.# to INT from VARCHAR You need to do the following:

SELECT SUM(ISNULL(CAST(Nvarchar_Col AS DECIMAL(6,2)),0)) as Total

This will fix both errors you are getting.

NOTE: I used 6 precision 2 (####.##) for your decimal but you can change this to whatever you need.

Upvotes: 2

Related Questions