Monday, March 19, 2012

Compare two column values with leading zeros

Hey,

This is what I would like to do:
===========
Declare @.chvBOLNumber
Set @.chvBOLNumber='0001234'
Select * from BOL where BOLNumber=@.chvBOLNumber
I want to return the row/rows when BOLNumber=1234
============

The problem is the leading zeros. @.chvBOLNumber can be 01234 or 001234 or ...

Hope the above makes sense. How can I do this ? (probably using wildcards)

Thanks, JohnPerhaps try an integer comparison instead by converting the number to
integer:

SELECT * from BOL where CONVERT(INTEGER, BOLNumber) = 1234

(no idea if this syntax is correct!)

"Girish" <kattukuyil@.hotmail.com> wrote in message
news:b2bb38a.0410060557.3479f77e@.posting.google.co m...
> Hey,
> This is what I would like to do:
> ===========
> Declare @.chvBOLNumber
> Set @.chvBOLNumber='0001234'
> Select * from BOL where BOLNumber=@.chvBOLNumber
> I want to return the row/rows when BOLNumber=1234
> ============
> The problem is the leading zeros. @.chvBOLNumber can be 01234 or 001234 or
> ...
> Hope the above makes sense. How can I do this ? (probably using wildcards)
> Thanks, John|||"Girish" <kattukuyil@.hotmail.com> wrote in message
news:b2bb38a.0410060557.3479f77e@.posting.google.co m...
> Hey,
> This is what I would like to do:
> ===========
> Declare @.chvBOLNumber
> Set @.chvBOLNumber='0001234'
> Select * from BOL where BOLNumber=@.chvBOLNumber
> I want to return the row/rows when BOLNumber=1234
> ============
> The problem is the leading zeros. @.chvBOLNumber can be 01234 or 001234 or
> ...
> Hope the above makes sense. How can I do this ? (probably using wildcards)
> Thanks, John

For questions like this, it's important to know the data types involved, but
assuming that BOLNumber is an integer, then you can try this:

Select *
from BOL
where BOLNumber = cast(@.chvBOLNumber as int)

If this doesn't work as expected, please post the data types of BOLNumber
and @.chvBOLNumber. In general, you should always try to post CREATE TABLE
and INSERT statements to provide some sample data - that way there's no
confusion over exactly what you need.

Simon

No comments:

Post a Comment