1

what is the difference between two following queries ?

WHERE DeviceID = '1339750411' and DT.McId = '40'

WHERE DeviceID = '1339750411' and DT.McId = 40

Both above query limitations return the same result.

McId is integer value in my SQL Server database.

Thanks for help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Night Walker
  • 20,638
  • 52
  • 151
  • 228

1 Answers1

13

Single quotes (' ') denote string literals in T-SQL.

The first WHERE clause has '40' as a string, but SQL Server will automagically convert that to int when comparing against an int column (DT.McId).

Option #2 is better: if it's an int column, there's no point in comparing against a string - it's an int, so treat it like an int.

Option #2 will not incur an implicit conversion and thus is better in terms of performance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Just to be clear, a single implicit conversion from a string variable to a number isn't going to be a big deal for performance. Implicit conversion usually refers to having to convert every value in the database to match the variable's datatype, which wouldn't happen here. – Brent Ozar Nov 16 '14 at 13:49
  • @BrentOzar: agreed - in this case here, they're will most likely be only very few comparisons - but it's just a generally bad idea to compare `int` values against string literals. – marc_s Nov 16 '14 at 14:14