Unix Technical Forum

SELECT query help - Zero padding

This is a discussion on SELECT query help - Zero padding within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I've got a field that stores numeric values, representing a tracking number. I've also got a stored procedure ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 05:06 AM
Rowland
 
Posts: n/a
Default SELECT query help - Zero padding

Hi,

I've got a field that stores numeric values, representing a tracking number.
I've also got a stored procedure that will extract this field and return it
to a client. However, I would like to return it slightly differently to the
way in which it is stored. Basically, I want to return it as TRK000nnn -
Where TRK is the string "TRK", 000 is zero-padding up to 4 zeros, and nnn is
the number itself - results would look something like this:

Tracking Number Formatted Value
1 TRK00001
24 TRK00024
43321 TRK43321

At the moment, a typical query could look something like this.

SELECT ("TRK" + CAST(trackNo AS varchar(10)))
FROM TB_Queue

But I'm not sure how to go about the zero padding. This would be easiest to
do on the client, but it is impractical (there are many client programs that
will use this stored procedure, and the format that it is returned as may
need to be altered in the future).

Many thanks,

Rowland.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 05:06 AM
Rowland
 
Posts: n/a
Default Re: SELECT query help - Zero padding

"Rowland" <banksr0@hotmail.com> wrote in message
news:ch45gj$r2o$1@titan.btinternet.com...
> Hi,
>
> I've got a field that stores numeric values, representing a tracking

number.
> I've also got a stored procedure that will extract this field and return

it
> to a client. However, I would like to return it slightly differently to

the
> way in which it is stored. Basically, I want to return it as TRK000nnn -
> Where TRK is the string "TRK", 000 is zero-padding up to 4 zeros, and nnn

is
> the number itself - results would look something like this:
>
> Tracking Number Formatted Value
> 1 TRK00001
> 24 TRK00024
> 43321 TRK43321
>
> At the moment, a typical query could look something like this.
>
> SELECT ("TRK" + CAST(trackNo AS varchar(10)))
> FROM TB_Queue
>
> But I'm not sure how to go about the zero padding. This would be easiest

to
> do on the client, but it is impractical (there are many client programs

that
> will use this stored procedure, and the format that it is returned as may
> need to be altered in the future).
>
> Many thanks,
>
> Rowland.
>

Ok - I've worked out one (ugly) way of doing it:
SELECT CAST(
("TRK" +
REPLICATE("0", 5 - LEN(CAST(trackNo AS varchar(10)))) +
CAST(trackNo AS varchar(10)))
AS VARCHAR (8))
AS trackNo
FROM TB_Queue
GO

If anyone knows a better way....


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 05:06 AM
MC
 
Posts: n/a
Default Re: SELECT query help - Zero padding

Just one comment, why do you cast trackNo to varchar in LEN function? I
dont think you need to do that....

SELECT CAST(
("TRK" +
REPLICATE("0", 5 - LEN(trackNo)) +
CAST(trackNo AS varchar(10)))
AS VARCHAR (8))
AS trackNo
FROM TB_Queue


MC

"Rowland" <banksr0@hotmail.com> wrote in message
news:ch4b34$45j$1@titan.btinternet.com...
> "Rowland" <banksr0@hotmail.com> wrote in message
> news:ch45gj$r2o$1@titan.btinternet.com...
>> Hi,
>>
>> I've got a field that stores numeric values, representing a tracking

> number.
>> I've also got a stored procedure that will extract this field and return

> it
>> to a client. However, I would like to return it slightly differently to

> the
>> way in which it is stored. Basically, I want to return it as TRK000nnn -
>> Where TRK is the string "TRK", 000 is zero-padding up to 4 zeros, and nnn

> is
>> the number itself - results would look something like this:
>>
>> Tracking Number Formatted Value
>> 1 TRK00001
>> 24 TRK00024
>> 43321 TRK43321
>>
>> At the moment, a typical query could look something like this.
>>
>> SELECT ("TRK" + CAST(trackNo AS varchar(10)))
>> FROM TB_Queue
>>
>> But I'm not sure how to go about the zero padding. This would be easiest

> to
>> do on the client, but it is impractical (there are many client programs

> that
>> will use this stored procedure, and the format that it is returned as may
>> need to be altered in the future).
>>
>> Many thanks,
>>
>> Rowland.
>>

> Ok - I've worked out one (ugly) way of doing it:
> SELECT CAST(
> ("TRK" +
> REPLICATE("0", 5 - LEN(CAST(trackNo AS varchar(10)))) +
> CAST(trackNo AS varchar(10)))
> AS VARCHAR (8))
> AS trackNo
> FROM TB_Queue
> GO
>
> If anyone knows a better way....
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 05:06 AM
Rowland
 
Posts: n/a
Default Re: SELECT query help - Zero padding

I'm not too sure why it needs it, but when I run the query without the cast
the output I get for trackNo in osql seems to be something like CHAR (1
trillion) (I exagerate, but not by much!). A cast to VARCHAR (10) just seems
to clear it up nicely. If anyone knows why I would be quite interested. It
seems to be due to the use of the REPLICATE function - perhaps some
undocumented behaviour - I wouldn't know as I didn't really read the docs
;-)

"MC" <marko_culo#@#yahoo#.#com#> wrote in message
news:ch4dr5$t02$1@brown.net4u.hr...
> Just one comment, why do you cast trackNo to varchar in LEN function? I
> dont think you need to do that....
>
> SELECT CAST(
> ("TRK" +
> REPLICATE("0", 5 - LEN(trackNo)) +
> CAST(trackNo AS varchar(10)))
> AS VARCHAR (8))
> AS trackNo
> FROM TB_Queue
>
>
> MC
>
> "Rowland" <banksr0@hotmail.com> wrote in message
> news:ch4b34$45j$1@titan.btinternet.com...
> > "Rowland" <banksr0@hotmail.com> wrote in message
> > news:ch45gj$r2o$1@titan.btinternet.com...
> >> Hi,
> >>
> >> I've got a field that stores numeric values, representing a tracking

> > number.
> >> I've also got a stored procedure that will extract this field and

return
> > it
> >> to a client. However, I would like to return it slightly differently to

> > the
> >> way in which it is stored. Basically, I want to return it as

TRK000nnn -
> >> Where TRK is the string "TRK", 000 is zero-padding up to 4 zeros, and

nnn
> > is
> >> the number itself - results would look something like this:
> >>
> >> Tracking Number Formatted Value
> >> 1 TRK00001
> >> 24 TRK00024
> >> 43321 TRK43321
> >>
> >> At the moment, a typical query could look something like this.
> >>
> >> SELECT ("TRK" + CAST(trackNo AS varchar(10)))
> >> FROM TB_Queue
> >>
> >> But I'm not sure how to go about the zero padding. This would be

easiest
> > to
> >> do on the client, but it is impractical (there are many client programs

> > that
> >> will use this stored procedure, and the format that it is returned as

may
> >> need to be altered in the future).
> >>
> >> Many thanks,
> >>
> >> Rowland.
> >>

> > Ok - I've worked out one (ugly) way of doing it:
> > SELECT CAST(
> > ("TRK" +
> > REPLICATE("0", 5 - LEN(CAST(trackNo AS varchar(10)))) +
> > CAST(trackNo AS varchar(10)))
> > AS VARCHAR (8))
> > AS trackNo
> > FROM TB_Queue
> > GO
> >
> > If anyone knows a better way....
> >
> >

>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 05:07 AM
-P-
 
Posts: n/a
Default Re: SELECT query help - Zero padding

I prefer using substring() into a string of zeroes.

SELECT
'TRK' +
Substring('000000', 1, 5 - LEN(CAST(trackNo AS varchar(10)))) +
CAST(trackNo AS varchar(10))
AS trackNo
FROM TB_Queue

--
Paul Horan[TeamSybase]

"Rowland" <banksr0@hotmail.com> wrote in message news:ch4b34$45j$1@titan.btinternet.com...
> "Rowland" <banksr0@hotmail.com> wrote in message
> news:ch45gj$r2o$1@titan.btinternet.com...
> > Hi,
> >
> > I've got a field that stores numeric values, representing a tracking

> number.
> > I've also got a stored procedure that will extract this field and return

> it
> > to a client. However, I would like to return it slightly differently to

> the
> > way in which it is stored. Basically, I want to return it as TRK000nnn -
> > Where TRK is the string "TRK", 000 is zero-padding up to 4 zeros, and nnn

> is
> > the number itself - results would look something like this:
> >
> > Tracking Number Formatted Value
> > 1 TRK00001
> > 24 TRK00024
> > 43321 TRK43321
> >
> > At the moment, a typical query could look something like this.
> >
> > SELECT ("TRK" + CAST(trackNo AS varchar(10)))
> > FROM TB_Queue
> >
> > But I'm not sure how to go about the zero padding. This would be easiest

> to
> > do on the client, but it is impractical (there are many client programs

> that
> > will use this stored procedure, and the format that it is returned as may
> > need to be altered in the future).
> >
> > Many thanks,
> >
> > Rowland.
> >

> Ok - I've worked out one (ugly) way of doing it:
> SELECT CAST(
> ("TRK" +
> REPLICATE("0", 5 - LEN(CAST(trackNo AS varchar(10)))) +
> CAST(trackNo AS varchar(10)))
> AS VARCHAR (8))
> AS trackNo
> FROM TB_Queue
> GO
>
> If anyone knows a better way....
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-29-2008, 05:07 AM
Hugo Kornelis
 
Posts: n/a
Default Re: SELECT query help - Zero padding

On Wed, 1 Sep 2004 11:16:52 +0000 (UTC), Rowland wrote:

>"Rowland" <banksr0@hotmail.com> wrote in message
>news:ch45gj$r2o$1@titan.btinternet.com...
>> Hi,
>>
>> I've got a field that stores numeric values, representing a tracking

>number.
>> I've also got a stored procedure that will extract this field and return

>it
>> to a client. However, I would like to return it slightly differently to

>the
>> way in which it is stored. Basically, I want to return it as TRK000nnn -
>> Where TRK is the string "TRK", 000 is zero-padding up to 4 zeros, and nnn

>is
>> the number itself - results would look something like this:
>>
>> Tracking Number Formatted Value
>> 1 TRK00001
>> 24 TRK00024
>> 43321 TRK43321
>>
>> At the moment, a typical query could look something like this.
>>
>> SELECT ("TRK" + CAST(trackNo AS varchar(10)))
>> FROM TB_Queue
>>
>> But I'm not sure how to go about the zero padding. This would be easiest

>to
>> do on the client, but it is impractical (there are many client programs

>that
>> will use this stored procedure, and the format that it is returned as may
>> need to be altered in the future).
>>
>> Many thanks,
>>
>> Rowland.
>>

>Ok - I've worked out one (ugly) way of doing it:
>SELECT CAST(
> ("TRK" +
> REPLICATE("0", 5 - LEN(CAST(trackNo AS varchar(10)))) +
> CAST(trackNo AS varchar(10)))
> AS VARCHAR (8))
>AS trackNo
>FROM TB_Queue
>GO
>
>If anyone knows a better way....


Hi Rowland,

Something like this? (I changed the column name to a variable for easier
testing)

declare @trackNo int
set @trackNo = 1
SELECT 'TRK' +
RIGHT('00000' + CAST(@trackNo AS varchar(5)),5)


Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-29-2008, 05:07 AM
Muzzy
 
Posts: n/a
Default Re: SELECT query help - Zero padding


"Rowland" <banksr0@hotmail.com> wrote in message
news:ch4hn5$bki$1@titan.btinternet.com...

> I'm not too sure why it needs it, but when I run the query without the

cast
> the output I get for trackNo in osql seems to be something like CHAR (1
> trillion) (I exagerate, but not by much!). A cast to VARCHAR (10) just

seems
> to clear it up nicely. If anyone knows why I would be quite interested. It
> seems to be due to the use of the REPLICATE function - perhaps some
> undocumented behaviour - I wouldn't know as I didn't really read the docs
> ;-)


Well, both isql and osql for a text field return as many characters as your
varchar is, adding spaces,
so if you have a field VARCHAR(300), but value is 'HELLO', isql/osql will
in fact return CHAR(300) with 5 characters as 'HELLO' and the rest in
spaces.

Why, i don't know, but i can guess that it's for output formatting purposes,
as it probably doesn't check
what is the longest value the field takes in reality

HIH
Andrey aka MuZZy


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:19 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com