Chris Diel
Chris Diel

Reputation: 23

query where '0'

I need to query a varchar field in sql for 0's.

when I query where field = '0' i get the resulting error message.

Conversion failed when converting the varchar value 'N' to data type int.

I'm having trouble figuring out where the issue is coming from. My Googling is failing me on this one, could someone point me in the right direction?

EDIT: Thanks for the help on this one guys, so there were 'N's in the data just very few of them so they weren't showing up in my top 100 query until I limited the search results further.

Apparently sql didn't have any issue comparing ints to varchar(1) so long as they were ints as well. I didn't even realize I was using an int in the where farther up in my query.

Oh and sorry for not sharing my query, it was long and complicated I was trying to share what I thought was the relevant from it. I'll write a simplified query in future questions.

Anyone know how to mark this as solved?

Upvotes: 1

Views: 352

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270463

If your field is a varchar(), then this expression:

where field = '0'

cannot return a type conversion error.

This version can:

where field = 0

It would return an error if field has the value of 'N'. I am guessing that is the situation.

Otherwise, you have another expression in your code causing the problem by doing conversions from strings to numbers.

Upvotes: 2

Related Questions