SELECT query help - Zero padding

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rowland

    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.


  • Rowland

    #2
    Re: SELECT query help - Zero padding

    "Rowland" <banksr0@hotmai l.com> wrote in message
    news:ch45gj$r2o $1@titan.btinte rnet.com...[color=blue]
    > Hi,
    >
    > I've got a field that stores numeric values, representing a tracking[/color]
    number.[color=blue]
    > I've also got a stored procedure that will extract this field and return[/color]
    it[color=blue]
    > to a client. However, I would like to return it slightly differently to[/color]
    the[color=blue]
    > 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[/color]
    is[color=blue]
    > 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[/color]
    to[color=blue]
    > do on the client, but it is impractical (there are many client programs[/color]
    that[color=blue]
    > will use this stored procedure, and the format that it is returned as may
    > need to be altered in the future).
    >
    > Many thanks,
    >
    > Rowland.
    >[/color]
    Ok - I've worked out one (ugly) way of doing it:
    SELECT CAST(
    ("TRK" +
    REPLICATE("0", 5 - LEN(CAST(trackN o AS varchar(10)))) +
    CAST(trackNo AS varchar(10)))
    AS VARCHAR (8))
    AS trackNo
    FROM TB_Queue
    GO

    If anyone knows a better way....


    Comment

    • MC

      #3
      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@hotmai l.com> wrote in message
      news:ch4b34$45j $1@titan.btinte rnet.com...[color=blue]
      > "Rowland" <banksr0@hotmai l.com> wrote in message
      > news:ch45gj$r2o $1@titan.btinte rnet.com...[color=green]
      >> Hi,
      >>
      >> I've got a field that stores numeric values, representing a tracking[/color]
      > number.[color=green]
      >> I've also got a stored procedure that will extract this field and return[/color]
      > it[color=green]
      >> to a client. However, I would like to return it slightly differently to[/color]
      > the[color=green]
      >> 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[/color]
      > is[color=green]
      >> 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[/color]
      > to[color=green]
      >> do on the client, but it is impractical (there are many client programs[/color]
      > that[color=green]
      >> will use this stored procedure, and the format that it is returned as may
      >> need to be altered in the future).
      >>
      >> Many thanks,
      >>
      >> Rowland.
      >>[/color]
      > Ok - I've worked out one (ugly) way of doing it:
      > SELECT CAST(
      > ("TRK" +
      > REPLICATE("0", 5 - LEN(CAST(trackN o AS varchar(10)))) +
      > CAST(trackNo AS varchar(10)))
      > AS VARCHAR (8))
      > AS trackNo
      > FROM TB_Queue
      > GO
      >
      > If anyone knows a better way....
      >
      >[/color]


      Comment

      • Rowland

        #4
        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#@#y ahoo#.#com#> wrote in message
        news:ch4dr5$t02 $1@brown.net4u. hr...[color=blue]
        > 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@hotmai l.com> wrote in message
        > news:ch4b34$45j $1@titan.btinte rnet.com...[color=green]
        > > "Rowland" <banksr0@hotmai l.com> wrote in message
        > > news:ch45gj$r2o $1@titan.btinte rnet.com...[color=darkred]
        > >> Hi,
        > >>
        > >> I've got a field that stores numeric values, representing a tracking[/color]
        > > number.[color=darkred]
        > >> I've also got a stored procedure that will extract this field and[/color][/color][/color]
        return[color=blue][color=green]
        > > it[color=darkred]
        > >> to a client. However, I would like to return it slightly differently to[/color]
        > > the[color=darkred]
        > >> way in which it is stored. Basically, I want to return it as[/color][/color][/color]
        TRK000nnn -[color=blue][color=green][color=darkred]
        > >> Where TRK is the string "TRK", 000 is zero-padding up to 4 zeros, and[/color][/color][/color]
        nnn[color=blue][color=green]
        > > is[color=darkred]
        > >> 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[/color][/color][/color]
        easiest[color=blue][color=green]
        > > to[color=darkred]
        > >> do on the client, but it is impractical (there are many client programs[/color]
        > > that[color=darkred]
        > >> will use this stored procedure, and the format that it is returned as[/color][/color][/color]
        may[color=blue][color=green][color=darkred]
        > >> need to be altered in the future).
        > >>
        > >> Many thanks,
        > >>
        > >> Rowland.
        > >>[/color]
        > > Ok - I've worked out one (ugly) way of doing it:
        > > SELECT CAST(
        > > ("TRK" +
        > > REPLICATE("0", 5 - LEN(CAST(trackN o AS varchar(10)))) +
        > > CAST(trackNo AS varchar(10)))
        > > AS VARCHAR (8))
        > > AS trackNo
        > > FROM TB_Queue
        > > GO
        > >
        > > If anyone knows a better way....
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • -P-

          #5
          Re: SELECT query help - Zero padding

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

          SELECT
          'TRK' +
          Substring('0000 00', 1, 5 - LEN(CAST(trackN o AS varchar(10)))) +
          CAST(trackNo AS varchar(10))
          AS trackNo
          FROM TB_Queue

          --
          Paul Horan[TeamSybase]

          "Rowland" <banksr0@hotmai l.com> wrote in message news:ch4b34$45j $1@titan.btinte rnet.com...[color=blue]
          > "Rowland" <banksr0@hotmai l.com> wrote in message
          > news:ch45gj$r2o $1@titan.btinte rnet.com...[color=green]
          > > Hi,
          > >
          > > I've got a field that stores numeric values, representing a tracking[/color]
          > number.[color=green]
          > > I've also got a stored procedure that will extract this field and return[/color]
          > it[color=green]
          > > to a client. However, I would like to return it slightly differently to[/color]
          > the[color=green]
          > > 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[/color]
          > is[color=green]
          > > 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[/color]
          > to[color=green]
          > > do on the client, but it is impractical (there are many client programs[/color]
          > that[color=green]
          > > will use this stored procedure, and the format that it is returned as may
          > > need to be altered in the future).
          > >
          > > Many thanks,
          > >
          > > Rowland.
          > >[/color]
          > Ok - I've worked out one (ugly) way of doing it:
          > SELECT CAST(
          > ("TRK" +
          > REPLICATE("0", 5 - LEN(CAST(trackN o AS varchar(10)))) +
          > CAST(trackNo AS varchar(10)))
          > AS VARCHAR (8))
          > AS trackNo
          > FROM TB_Queue
          > GO
          >
          > If anyone knows a better way....
          >
          >[/color]


          Comment

          • Hugo Kornelis

            #6
            Re: SELECT query help - Zero padding

            On Wed, 1 Sep 2004 11:16:52 +0000 (UTC), Rowland wrote:
            [color=blue]
            >"Rowland" <banksr0@hotmai l.com> wrote in message
            >news:ch45gj$r2 o$1@titan.btint ernet.com...[color=green]
            >> Hi,
            >>
            >> I've got a field that stores numeric values, representing a tracking[/color]
            >number.[color=green]
            >> I've also got a stored procedure that will extract this field and return[/color]
            >it[color=green]
            >> to a client. However, I would like to return it slightly differently to[/color]
            >the[color=green]
            >> 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[/color]
            >is[color=green]
            >> 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[/color]
            >to[color=green]
            >> do on the client, but it is impractical (there are many client programs[/color]
            >that[color=green]
            >> will use this stored procedure, and the format that it is returned as may
            >> need to be altered in the future).
            >>
            >> Many thanks,
            >>
            >> Rowland.
            >>[/color]
            >Ok - I've worked out one (ugly) way of doing it:
            >SELECT CAST(
            > ("TRK" +
            > REPLICATE("0", 5 - LEN(CAST(trackN o AS varchar(10)))) +
            > CAST(trackNo AS varchar(10)))
            > AS VARCHAR (8))
            >AS trackNo
            >FROM TB_Queue
            >GO
            >
            >If anyone knows a better way....[/color]

            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)

            Comment

            • Muzzy

              #7
              Re: SELECT query help - Zero padding


              "Rowland" <banksr0@hotmai l.com> wrote in message
              news:ch4hn5$bki $1@titan.btinte rnet.com...
              [color=blue]
              > I'm not too sure why it needs it, but when I run the query without the[/color]
              cast[color=blue]
              > 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[/color]
              seems[color=blue]
              > 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
              > ;-)[/color]

              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


              Comment

              Working...