Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to display column_names based on condition Expand / Collapse
Author
Message
Posted Tuesday, January 08, 2013 2:25 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-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
Post #1404070
Posted Tuesday, January 08, 2013 2:51 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen 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..
Post #1404080
Posted Tuesday, January 08, 2013 4:38 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 3:14 AM
Points: 116, Visits: 355
Query Please....
Post #1404150
Posted Tuesday, January 08, 2013 4:51 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-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...
Post #1404159
Posted Tuesday, January 08, 2013 5:19 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-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...
Post #1404174
Posted Tuesday, January 08, 2013 5:22 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-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...
Post #1404176
Posted Tuesday, January 08, 2013 5:26 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-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.
Post #1404178
Posted Tuesday, January 08, 2013 6:46 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-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...
Post #1404210
Posted Tuesday, January 08, 2013 6:50 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 8:21 AM
Points: 5,602, Visits: 10,950
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...


SELECT Col2
FROM mytable
WHERE Col1 = 100


“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1404216
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse