Results 1 to 7 of 7
  1. #1
    Join Date
    May 2003
    Posts
    40

    Unanswered: column concatenation

    Hi

    Assume a query :

    select bankno,regionno,officeno from afo

    The above query displays three columns (grids) in query analyzer. Is there in any way that
    we can concatenate the above three columns and display as only one column in the grid with a hyphen as separator.



    Thanx

  2. #2
    Join Date
    Apr 2003
    Location
    Israel
    Posts
    81
    Try:

    select
    convert(varchar,bankno) +
    convert(varchar,regionno)+
    convert(varchar,officeno)
    from afo
    --
    kukuk

  3. #3
    Join Date
    Jan 2003
    Location
    Nottinghamshire, UK
    Posts
    364
    Or Even

    select
    convert(varchar,bankno) + '-' +
    convert(varchar,regionno) + '-' +
    convert(varchar,officeno)
    from afo

    convert(varchar(50),'I''m only any good with the easy ones' + ' - ' + 'lol')

    GW
    "Everything should be made as simple as possible, but not simpler." - Albert Einstein
    "Everything should be made as complex as possible, so I look Cleverer." - Application Developer

  4. #4
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809
    kir441, be aware that when using convert(varchar,<table attribute>) you are converting to a varchar(30) datatype. While this is good enough for Integers it could cause truncation for other datatypes.

    Nothing like the obveous GWilliy, but why didn't you use CAST?
    Paul Young
    (Knowledge is power! Get some!)

  5. #5
    Join Date
    May 2003
    Posts
    40
    Paul & Gwilliy

    I used convert(varchar,bankno) + '-' + convert(varchar,acctno) ....etc
    for my query. It worked, as you suggested we can use CAST also.
    Thanks for your answers.

  6. #6
    Join Date
    Jan 2003
    Location
    Nottinghamshire, UK
    Posts
    364

    Re :- but why didn't you use CAST?

    U Mean

    CAST('I''m only any good with the easy ones' + ' - ' + 'lol' AS VarChar(50))
    (notice the not so obvious in both methods ?)

    Dunno really Paul -

    Guess I'm just used to using Convert and it feels a little more english to me - not forgetting the ability to use the style option.

    Is there a good reason why I should start using CAST instead ?

    GW
    "Everything should be made as simple as possible, but not simpler." - Albert Einstein
    "Everything should be made as complex as possible, so I look Cleverer." - Application Developer

  7. #7
    Join Date
    Feb 2002
    Location
    Houston, TX
    Posts
    809
    My philosophy is use what works and is most maintainable in your shop.
    Paul Young
    (Knowledge is power! Get some!)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •