Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Int and Varchar (SQL Server) in Where clause?

What is the difference between AID = 10 or AID = '10' when AID is an INT type in SQL Server?

Select *
from ATable
where AID = '10'

and

Select *
from ATable
where AID = 10
like image 778
Mohamad Avatar asked Apr 27 '26 14:04

Mohamad


1 Answers

The difference is that SQL will need to do conversion in order to compare the varchar constant to the type of your column.

If you show the execution plan for the first query , you will see something like:

CONVERT_IMPLICIT(int, [@1], 0)

Edit

In the case above, conversion of a constant will only have a small performance impact. However, if the conversion is done instead on the column (and not the constant), this can result in scans which can have more serious consequences.

As per Remus answer, you can check the order of precedence of conversion here

like image 55
StuartLC Avatar answered Apr 29 '26 03:04

StuartLC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!