2
SELECT CASE Price WHEN '' THEN 0.0 ELSE Price END FROM Table

If price empty, it returns 0.0 but if column has value it gives me the error below

Arithmetic overflow error converting varchar to data type numeric.

How to change my code to handle this case?

3
  • what data type is the Price column? Commented Sep 22, 2011 at 1:04
  • Then how can it equal an empty string? I don't understand your CASE statement. Commented Sep 22, 2011 at 1:06
  • I'm selecting it from one table where price is string and inserting it in another table where the price is decimal Commented Sep 22, 2011 at 1:09

1 Answer 1

3
SELECT CASE Price WHEN '' THEN 0.0 ELSE CAST(Price AS DECIMAL(...)) END FROM Table

Implicit conversions will often take the smallest type possible, so when you hit a larger value it will fail. Explicitly convert/cast your value to the right type in your CASE/ELSE.

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

1 Comment

it works, some of the columns have value like 30.100000000000001 and it fails. Is there any workarounds?

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.