473,322 Members | 1,307 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,322 software developers and data experts.

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.
Jul 20 '05 #1
6 17852
"Rowland" <ba*****@hotmail.com> wrote in message
news:ch**********@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....
Jul 20 '05 #2
MC
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" <ba*****@hotmail.com> wrote in message
news:ch**********@titan.btinternet.com...
"Rowland" <ba*****@hotmail.com> wrote in message
news:ch**********@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....

Jul 20 '05 #3
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:ch**********@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" <ba*****@hotmail.com> wrote in message
news:ch**********@titan.btinternet.com...
"Rowland" <ba*****@hotmail.com> wrote in message
news:ch**********@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....


Jul 20 '05 #4
-P-
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" <ba*****@hotmail.com> wrote in message news:ch**********@titan.btinternet.com...
"Rowland" <ba*****@hotmail.com> wrote in message
news:ch**********@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....

Jul 20 '05 #5
On Wed, 1 Sep 2004 11:16:52 +0000 (UTC), Rowland wrote:
"Rowland" <ba*****@hotmail.com> wrote in message
news:ch**********@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)
Jul 20 '05 #6

"Rowland" <ba*****@hotmail.com> wrote in message
news:ch**********@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
Jul 20 '05 #7

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

Similar topics

5
by: Marita | last post by:
Hi, I have a page (several) where the top margin doesn't appear to be working. I have this in my code: <style type="text/css"> <!-- body { margin-left: 0px; margin-top: 0px; margin-right:...
3
by: Radde | last post by:
HI all, #include<iostream.h> class A { //char i; }; class B
15
by: I wish | last post by:
#include <string.h> int a; memset( a, 0, sizeof(a) ); Does that guarantee all bits zero? -- |
258
by: Terry Andersen | last post by:
If I have: struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_{ unsigned int two_1;
1
by: Aaron | last post by:
I need a query that can return wildcard matches if there's no exact match. There's how the process works. 1. Input = "my email address is kfjsk@fsdf.com" 2. Look for exact match. 3. Look for...
4
by: Amy | last post by:
I need some help. I have this table with alternate row colors. Class gray and class white. I have javascript that do highlight when mouseover row ... and onclick to select row and highlight it...
6
by: Patrick | last post by:
Hello All, Kind of new to PHP. I have an html form where a person inputs a date via a drop down select. How do I make php put a zero in front of the numbers 1-9 ie. 01 02 03 when writing to the...
6
by: Army1987 | last post by:
Reliable sources (whose names I'm not allowed to disclose) told me that on the next version of the Deathstation (version 10000, or DS10K) an integral type (they didn't tell which) will have a odd...
1
by: ags5406 | last post by:
i have a normal label with a fixed width font with no padding... lblsizing.Font {Name = "Lucida Console" Size=12.0} System.Drawing.Font lblsizing.Padding.All 0 Integer but despite the fact...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.