SQL Server Select SUM

I am getting error "datatype nvarchar(max) is invalid for sum...".  

SELECT SUM([ProductTaxonomy.csv].[YTD Sales Amount])
FROM [ProductTaxonomy.csv]
WHERE NOT EXISTS
(SELECT [Product Management.csv].[Product_Code] FROM [Product Management.csv] WHERE [ProductTaxonomy.csv].[Product Code] = [Product Management.csv].[Product_Code])
hojohappyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
SELECT SUM( CAST([ProductTaxonomy.csv].[YTD Sales Amount] as decimal(18,4)) )
FROM [ProductTaxonomy.csv]
WHERE NOT EXISTS
(SELECT [Product Management.csv].[Product_Code] FROM [Product Management.csv] WHERE [ProductTaxonomy.csv].[Product Code] = [Product Management.csv].[Product_Code])

But this assumes that column is all numbers, & I have guessed that decimal(18,4) is appropriate
0
Anthony PerkinsCommented:
I would take it a step further:
SELECT  SUM(CASE ISNUMERIC([YTD Sales Amount])
              WHEN 1 THEN [YTD Sales Amount]
              ELSE 0
            END)
FROM    [ProductTaxonomy.csv] t
WHERE   NOT EXISTS ( SELECT 1
                     FROM   [Product Management.csv] m
                     WHERE  t.[Product Code] = m.Product_Code )

Open in new window


And yes, I understand that ISNUMERIC() is flawed at best, but it is better than nothing.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
of course, doh! what was I thinking - sorry
0
chaauCommented:
BTW, SQL Server has TRY_CAST (and TRY_CONVERT) that allow to avoid using CASES altogether:
SELECT  SUM(TRY_CAST([YTD Sales Amount] AS decimal(18,4)))
FROM    [ProductTaxonomy.csv] t
WHERE   NOT EXISTS ( SELECT 1
                     FROM   [Product Management.csv] m
                     WHERE  t.[Product Code] = m.Product_Code )

Open in new window

Note: they exist in SQL Server 2012 and above
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

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.