Greg Gum
Greg Gum

Reputation: 37875

Why does this give `error converting data type varchar to numeric`

I am having a SQL issue, and I have boiled it down to this:

if isnull(5.5,'') = isnull(null,'') select 0 else select 1

which throws the error Error Converting data type varchar to numeric.

Why is this throwing this error, and how to resolve?

Upvotes: 0

Views: 1271

Answers (1)

Joshua R.
Joshua R.

Reputation: 2302

This simpler expression (which is equivalent to your expression) gives the same error:

if 5.5 = '' select 0 else select 1

You're comparing a varchar to a numeric type as the error indicates.

You can CAST your expression to match types:

if CAST(isnull(5.5,'') AS VARCHAR)  = isnull(null,'') select 0 else select 1

Upvotes: 2

Related Questions