473,387 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Int to char conversion

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

Feb 2 '06 #1
4 12263

ji**********@countrywide.com wrote:
It looks like the conversion is to '234' and the match
fails.


Doesn't that answer your question?

Feb 2 '06 #2
Not sure exactly what your questiuon is ...but you may want to post
your could...might be helpful

MJKulangara
http://sqladventures.blogspot.com

Feb 2 '06 #3
On 2 Feb 2006 10:04:18 -0800, ji**********@countrywide.com wrote:
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.


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
Feb 2 '06 #4
(ji**********@countrywide.com) writes:
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.


Yes, you would need to pad the input parameter with leading zeroes.
--
Erland Sommarskog, SQL Server MVP, es****@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
Feb 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
10
by: kevin.hall | last post by:
GCC 3.3 and MSVS 6.0 have no problem converting char* to const char* (not even a warning), but MS's WinCE compiler generated an error complained that this was not possible. MS's WinCE compiler did...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
3
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string>...
8
by: AGRAJA | last post by:
how to convert unsigned to char? Ref: http://www.thescripts.com/forum/thread477545.html how do I print without the leading ffffff (yet the result should be char*)?
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.