Connecting Tech Pros Worldwide Forums | Help | Site Map

Int to char conversion

jim_geissman@countrywide.com
Guest
 
Posts: n/a
#1: Feb 2 '06
I have data tables that include ZIP code, as char(5). The values look
like integers, but they are padded with leading zeroes to fill out 5
characters, '00234'.

There are SPs to look up data, with @Zip char(5) among the parameters.
Some users call these with integer values, @Zip = 234, and SQL makes
the conversion. Is it necesary to add the leading zeroes in the SP --
@Zip=RIGHT('00000'+@Zip,5) -- or would SQL find this match? (234 =
'00234'). It looks like the conversion is to '234' and the match
fails.

Thanks,
Jim Geissman
Countrywide


ZeldorBlat
Guest
 
Posts: n/a
#2: Feb 2 '06

re: Int to char conversion



jim_geissman@countrywide.com wrote:[color=blue]
>It looks like the conversion is to '234' and the match
> fails.[/color]

Doesn't that answer your question?

MJKulangara
Guest
 
Posts: n/a
#3: Feb 2 '06

re: Int to char conversion


Not sure exactly what your questiuon is ...but you may want to post
your could...might be helpful

MJKulangara
http://sqladventures.blogspot.com

Hugo Kornelis
Guest
 
Posts: n/a
#4: Feb 2 '06

re: Int to char conversion


On 2 Feb 2006 10:04:18 -0800, jim_geissman@countrywide.com wrote:
[color=blue]
>I have data tables that include ZIP code, as char(5). The values look
>like integers, but they are padded with leading zeroes to fill out 5
>characters, '00234'.
>
>There are SPs to look up data, with @Zip char(5) among the parameters.
>Some users call these with integer values, @Zip = 234, and SQL makes
>the conversion. Is it necesary to add the leading zeroes in the SP --
>@Zip=RIGHT('00000'+@Zip,5) -- or would SQL find this match? (234 =
>'00234'). It looks like the conversion is to '234' and the match
>fails.[/color]

Hi Jim,

Short answer: Never rely on implicit conversion.

Long answer: if @Zip is an integer and the column is char(5), then for a
comparison, the char(5) data in the column will be converted to integer
(look up "data type precedence" in Books Online). This is bad for
several reasons:

1. Unexpected data in the column might cause errors, causing the query
to be aborted.

2. The values in all rows have to be converted, which is slow.

3. If an index on the zip column exists, it can't be used because the
data has to be converted.


So you should definitely ensure that the parameter is converted to the
exact same datatype as the column (i.e. CHAR(5)) before comparing.

And yes - in string comparisons, '00234' is not the same as '234'.

--
Hugo Kornelis, SQL Server MVP
Erland Sommarskog
Guest
 
Posts: n/a
#5: Feb 2 '06

re: Int to char conversion


(jim_geissman@countrywide.com) writes:[color=blue]
> I have data tables that include ZIP code, as char(5). The values look
> like integers, but they are padded with leading zeroes to fill out 5
> characters, '00234'.
>
> There are SPs to look up data, with @Zip char(5) among the parameters.
> Some users call these with integer values, @Zip = 234, and SQL makes
> the conversion. Is it necesary to add the leading zeroes in the SP --
> @Zip=RIGHT('00000'+@Zip,5) -- or would SQL find this match? (234 =
> '00234'). It looks like the conversion is to '234' and the match
> fails.[/color]

Yes, you would need to pad the input parameter with leading zeroes.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Closed Thread


Similar Microsoft SQL Server bytes