Re: use a quote for integer data type in SQL statement?
From: David Portas (REMOVE_BEFORE_REPLYING_dportas_at_acm.org)
Date: 09/18/04
- Next message: DBA: "Need to move tempdb"
- Previous message: Mike Sutton: "Re: Relationship Management Idea"
- In reply to: Matt: "use a quote for integer data type in SQL statement?"
- Next in thread: David Portas: "Re: use a quote for integer data type in SQL statement?"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 18 Sep 2004 18:59:07 +0100
If you compare a string value to an integer value then the string value will
be implicitly converted to an integer. This means that if you specify
something like ID='1001' where ID is an integer then '1001' gets converted
(provided the literal string represents a valid number) and the expression
will work as you might have expected.
On the other hand if ID is a CHAR/VARCHAR column and you write ID=1001 then
*every value* in the ID column has to converted to an integer in order for
the comparison to take place. This can be a significant performance hit and
will also result in an error if any row in the table contains an ID that
isn't valid for numeric conversion. For these reasons you should always try
to avoid unnecessary data type converions. Specify literal values correctly:
1001 for integers, '1001' for strings.
For more information see the Data Type Precedence topic in Books Online.
Hope this helps.
-- David Portas SQL Server MVP --
- Next message: DBA: "Need to move tempdb"
- Previous message: Mike Sutton: "Re: Relationship Management Idea"
- In reply to: Matt: "use a quote for integer data type in SQL statement?"
- Next in thread: David Portas: "Re: use a quote for integer data type in SQL statement?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|