SQL Error - expecting integer but finding nvarchar

jamesmetcalf74
jamesmetcalf74 used Ask the Experts™
on
Problem with Query - help with diagnosing error in sql.
Sql management studio provides me with an error message when i run a query.  If some one knows what the problem is... rogue data in a column that shouldnt be there... GREAT!
But i would also want to know...  a method for figuring this out... hopefully a method that resides in management studio.

error and query is attached

sql profiler etc...
error.JPG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Developer / Linux System Administrator / Managing Director
Commented:
It appears to me that you have a column that contains a value that is defined as a character type and it contains the value "one" but you are comparing with an integer value. "One" cannot be converted to an integer representation. Are APPPAYKEY or APNO defined as a character? Can you include the table definitions?
Most Valuable Expert 2012
Distinguished Expert 2018

Commented:
I assume SQL Server?

To locate the "bad data, look at the ISNUMERIC function:
https://docs.microsoft.com/en-us/sql/t-sql/functions/isnumeric-transact-sql?view=sql-server-2017

You didn't provide the nvarchar column or which table it is in so something like:
select some_column from some_table where ISNUMERIC(some_column) <> 1;
Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016

Commented:
Easiest way IMO is just open the tables in SSMS and look at the column data types and see what doesn't match.  Or pop open designer and drop in the tables to get an overview.

sqlcolumntypes.png
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Most Valuable Expert 2012
Distinguished Expert 2018
Commented:
I might have misread the question but if I move the statements around a little, I thought it was:
"a method for figuring out rogue data in a column that shouldnt be there"
Dustin SaundersCo-Founder and Chief Architect
Top Expert 2016

Commented:
@slightwv I think you're right and I mistook the question.

Is there a reason why constraints can't be introduced to your column?  If you go clean it up, how did the bad data get there and what happens when bad data gets inserted again?

Author

Commented:
Thanks Gentlemen -
both techniques were valuable

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial