Results 1 to 12 of 12
  1. #1
    Join Date
    Jul 2007
    Posts
    9

    Unanswered: To get Column name

    Hello everybody,

    i am sukumar n i m new to tis forum. I want to get column name in which desired value is present

    for an instance if value 'PAL' is present in a column 'ITEM' , i should get ITEM as result.

    regards,
    suku

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Is this a .NET question or a SQL one?
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  3. #3
    Join Date
    Jul 2007
    Posts
    9
    Quote Originally Posted by sukumaster
    Hello everybody,

    i am sukumar n i m new to tis forum. I want to get column name in which desired value is present

    for an instance if value 'PAL' is present in a column 'ITEM' , i should get ITEM as result.

    regards,
    suku


    ITS A SQL SERVER QUESTION I WANT SOUTIONS ONLY BY USING SQL SERVER

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    WELL YOU ARE NOT GOING TO GET THEM BY YELLING AT THE REGULARS
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #5
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,799
    Provided Answers: 11
    It is a bad database design, but there are solutions out there. CPU intensive, poorly performing solutions. Sadly, I do not have one on hand.

    EDIT: Try looking over on sqlteam.com. Last time I saw this script, it was hosted on a blog out there, somewhere.
    Last edited by MCrowley; 07-24-07 at 17:05.

  6. #6
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Well you are going to have to be more specific as we can only gues

    Code:
    SELECT ITEM FROM (
       SELECT 'ITEM' AS ITEM, 1 AS RowOrder
    UNION ALL
       SELECT ITEM, 2 AS RowOrder 
         FROM Table
        WHERE ITEM = 'PAL') AS XXX
     ORDER BY 2, 1
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  7. #7
    Join Date
    Jul 2007
    Posts
    9

    thx

    its working
    Thx a ton

  8. #8
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    If I may quote:
    Quote Originally Posted by r937
    pootle, you are a beacon of sanity, the voice of reason in the pursuit of simplicity
    Code:
    SELECT COALSCE(NULLIF(Item, 'PAL'), 'ITEM') AS ITEM
    Testimonial:
    pootle flump
    ur codings are working excelent.

  9. #9
    Join Date
    Feb 2003
    Location
    India
    Posts
    216
    i could not understand what is needed here, though the reply got "its working" certificate. apart from small typo errors (Brett's solution tries to order on two cols while selects only one, pootle's has spelling mistake COALSCE) the solutions are too different and the requirement is strange. if we need to know if ITEM column is having the text 'PAL' a this is not a dynamic requirement where we will have to list all the columns that is having 'PAL' we can simply issues a select to do that. or why this will not do?

    select case when exists (select item from table where item='PAL') then 'item' else 'nops' end

  10. #10
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Quote Originally Posted by upalsen
    i could not understand what is needed here, though the reply got "its working" certificate. apart from small typo errors (Brett's solution tries to order on two cols while selects only one, pootle's has spelling mistake COALSCE) the solutions are too different and the requirement is strange. if we need to know if ITEM column is having the text 'PAL' a this is not a dynamic requirement where we will have to list all the columns that is having 'PAL' we can simply issues a select to do that. or why this will not do?

    select case when exists (select item from table where item='PAL') then 'item' else 'nops' end

    My interpretation....he wants a "header" row in a result set, most likely so he can just display it in a data grid. I would imagine that this would allow him to dynamically reuse the same grid....better lock that first row though

    Typo?
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  11. #11
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Quote Originally Posted by sukumaster
    its working
    Thx a ton
    Good Guess, I Guess

    Code:
    CREATE TABLE myTable99(ITEM varchar(10))
    GO
    
    INSERT INTO myTable99(ITEM)
    SELECT 'PAL' UNION ALL SELECT 'Buddy' UNION ALL SELECT 'GFreind'
    GO
    
    
    SELECT ITEM FROM (
       SELECT 'ITEM' AS ITEM, 1 AS RowOrder
    UNION ALL
       SELECT ITEM, 2 AS RowOrder 
         FROM myTable99
        WHERE ITEM = 'PAL') AS XXX
     ORDER BY RowOrder, ITEM 
    GO
    
    DROP TABLE myTable99
    GO
    No typo's here
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  12. #12
    Join Date
    Feb 2003
    Location
    India
    Posts
    216
    right, no typo's now... got fixed by "ORDER BY RowOrder, ITEM" ...

Posting Permissions

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