• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3390
  • Last Modified:

SQL SUM of nvarchar to decimal

Hi everyone,

I have to work with some data that unfortunately is not very friendly in terms of allowing to use SUM. This is what I am trying to do ...

SELECT SUM(CAST(BidValue AS decimal)) AS BidValue FROM Portfolios

BidValue is nvarchar data type. There is nothing I can do to change this, but the issue I am having is I need it to return the decimal places, not round the returned value up or down.

Is there another data type I need to cast to, or is there something I need to add to this?

Thanks in advance.
0
DaiWilliams
Asked:
DaiWilliams
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
simply:

SELECT SUM(CAST(BidValue AS decimal(20,5) )) AS BidValue FROM Portfolios

Open in new window

0
 
Mohit VijayCommented:
SELECT SUM(CAST(BidValue AS decimal(15,7) )) AS BidValue FROM Portfolios
WHERe BidValue IS NOT NULL OR BidValue <> ''

7 is decimal places
0
 
Mohit VijayCommented:
A Correction..


SELECT SUM(CAST(BidValue AS decimal(15,7) )) AS BidValue FROM Portfolios
WHERe BidValue IS NOT NULL AND BidValue <> ''

7 is decimal places
0
 
Priya PerumpilavilSoftware EngineerCommented:
try this
SELECT  sum( convert (decimal,BidValue)) AS BidValue FROM Portfolios
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now