Convert Row values to Columns

  • Hi Guys,

    I have table which retrives values and i want to convert those values to columns but the values are not fixed it may increase.

    example

    ID TEXT

    ------------------------

    1 US

    2 UK

    3 SINGAPORE

    etc....

    Required Result.

    US UK SINGAPORE 'ETC.......

    Please advise for a single query without using any temp tables,SP's or functions others...

    Please advise.

  • Since you're using SQL 2005, look up Pivot in books online.

    Does exactly what you're looking for.

    p.s. Why the restriction on not using temp tables, functions, etc?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • ravindra.gangadhar (12/17/2007)


    Hi Guys,

    I have table which retrives values and i want to convert those values to columns but the values are not fixed it may increase.

    example

    ID TEXT

    ------------------------

    1 US

    2 UK

    3 SINGAPORE

    etc....

    Required Result.

    US UK SINGAPORE 'ETC.......

    Please advise for a single query without using any temp tables,SP's or functions others...

    Please advise.

    So you want to see 10K columns if there are 10K rows? [;)]


    Madhivanan

    Failing to plan is Planning to fail

  • Yes, If 10K rows then i want 10K columns. Thanks

  • That's going to be difficult - SQL2005 only allows for 1024 columns (in 32-bit) or 4096 (in 64-bit) per table, and 4096 per select statement.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • The closest you're going to get is this tool,

    http://www.geckoware.com.au/Content.aspx?Doc_id=1002

    If that doesn't meet your needs then its time to re-evaluate your desired outcomes.

    I mean really, who is going to look a 10K columns ... :ermm::unsure:

    --------------------
    Colt 45 - the original point and click interface

  • Hi

    Using privot is a good idea. since the number of columns can change your pivot query can be a dynamic sql.

    "Keep Trying"

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply