Click here to Skip to main content
15,035,521 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there,

When I make a SELECT query, i want to take a row that has a condition in 'WHERE'  clause. That value must be varchar, I put an int value, but the problem is when a runn a query I get this message:

Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value 'Krokodilli' to data type int.

So, I never type something like  'Krokodilli' in my query, I jut put some numbers. I don't know where SQL takes this word 'Krokodilli', instead of showing some number like 52488 that I type.
 
Anybody knows whats is going on? 


What I have tried:

I did not try anything. i am just looking for any sollucions
Posted
Updated 3-Jun-21 3:17am
Comments
phil.o 26-Feb-18 4:26am
   
Show the code.
Suvendu Shekhar Giri 26-Feb-18 4:32am
   
Please share the query.
Suvendu Shekhar Giri 26-Feb-18 4:34am
   
Mostly, this value should be there in the table, I believe. May be test data.

The word "Krokodilli" doesn't come from your query, it comes from your data - and it's in the column that you are trying to compare.

We can't see your code - or your data - so we can't tell you exactly what to do to fix this, but part of your WHERE clause is causing SQL to try and convert your column to an integer value and failing.
Look closely at it, and try putting quotes around any litteral values you want to compare, or using CONVERT to change parameter values to NVARCHAR for comparison: CAST and CONVERT (Transact-SQL) | Microsoft Docs[^]
   
Quote:
So, I never type something like 'Krokodilli' in my query, I jut put some numbers. I don't know where SQL takes this word 'Krokodilli', instead of showing some number like 52488 that I type.
That is all what SQL injection is about. Your SQL server start to execute some code that you didn't typed in the query.

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900