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 ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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. |
| |||
| "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.... |
| |||
| 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.... > > |
| |||
| 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.... > > > > > > |
| |||
| 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.... > > |
| |||
| 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) |
| ||||
| "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 |