1

i have field data that consist of NULL. I try to get SUM data by making NULL data as 0.00.

I use

select Sum(cast(ISNULL (amount,0) as money)) from t

here the demo

How to avoid this error "Cannot convert a char value to money. The char value has incorrect syntax"

1
  • 1
    why on earth would anyone store currency as a string? Everytime you do, a puppy dies. Commented Dec 28, 2013 at 2:28

2 Answers 2

5

Your NULL is not a null value, but rather a string with the word 'NULL' in it:

SQL Fiddle

When you change it to a real NULL as per the demo, this will work without an ISNULL() since aggregates ignore NULL values:

SELECT SUM(CAST(amount as MONEY)) 
FROM t

If you need to work around that string value, use REPLACE() instead of ISNULL():

SELECT SUM(CAST(REPLACE(amount,'NULL',0) as MONEY)) 
FROM t

Best practice would of course be to not store numeric as strings, but sadly we cannot always control the data we get.

Sign up to request clarification or add additional context in comments.

1 Comment

"but sadly we cannot always control the data we get" - no, but when you have it you can.
2

In your demo, you don't actually have a null value in the last record, you have a character string called 'NULL'. Change it to an actual null value and it should work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.