|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 3:14 AM
Points: 116,
Visits: 355
|
|
Hi Sql Server Team,
HAPPY NEW YEAR.
--
Am having below table
Table1
Col_1 Col2 Col3 ---------------------------------------- 100 Prod Ext 101 Sales Exd 102 Mark Tet 103 Purc Ket
i want to query to display the column_names
Eg_1: if table1.col_1 contains 100 then output should be Col_1 Eg_2: if table1.col_3 contains Tet then output should be Col_3
based on my condtion column_name should be displayed as output.
Please help
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 10:01 PM
Points: 1,044,
Visits: 226
|
|
| Best way is to use union for all condition..
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 3:14 AM
Points: 116,
Visits: 355
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:02 AM
Points: 403,
Visits: 904
|
|
think this is what you want....
create table t1 (col1 varchar(10), col2 varchar(10), col3 varchar(10))
insert into t1 values ('100', 'Prod', 'Ext'), ('101', 'Sales', 'Exd'), ('102', 'Mark', 'Tet'), ('103', 'Purc', 'Ket')
SELECT CASE WHEN col1 = '100' THEN 'col1' ELSE col1 END col1, col2, CASE WHEN col3 = 'Tet' THEN 'col3' ELSE col3 END col3 FROM t1
Pedro
If you need to work better, try working less...
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 3:14 AM
Points: 116,
Visits: 355
|
|
Sorry,,,
Small change in my requeirement...
Table_Name : Table1
Col_1 Col_2 ---------------------- 100 Prod 101 Sales 102 Mark 103 Purc
if exists (select 1 from Table1 where Col_1=100) then i want to get the corresponding column value of 100, that is "Prod" as output.
-- Query please...
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:02 AM
Points: 403,
Visits: 904
|
|
Minnu (1/8/2013) Sorry,,,
Small change in my requeirement...
Table_Name : Table1
Col_1 Col_2 ---------------------- 100 Prod 101 Sales 102 Mark 103 Purc
if exists (select 1 from Table1 where Col_1=100) then i want to get the corresponding column value of 100, that is "Prod" as output.
-- Query please...
Can you post the query result?! I don't quite understand what you want..
Pedro
If you need to work better, try working less...
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 3:14 AM
Points: 116,
Visits: 355
|
|
My Requirement is : I have to check whether col_1 contains 100, if Yes then insert corresponding value (i.e Prod) into another table.
like that i've to search for multiple records.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:02 AM
Points: 403,
Visits: 904
|
|
Minnu (1/8/2013) My Requirement is : I have to check whether col_1 contains 100, if Yes then insert corresponding value (i.e Prod) into another table.
like that i've to search for multiple records.
Then a simple:
INSERT INTO tabledestination SELECT WhatEver FROM table1 WHERE Col1 = 100
will do the trick... Just insert in you destination table there records that have 100 on col1 from table1.
Pedro
If you need to work better, try working less...
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 8:21 AM
Points: 5,602,
Visits: 10,950
|
|
|
|
|