Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Selecting a value based on values in different col

Author  Topic 

vikashsql
Starting Member

6 Posts

Posted - 2008-05-12 : 04:02:26
I'v a table
Table1
With columns
Col1
Col2
Col3
Col4

Now i'm populating data from this table to an excel sheet , there is one column 'Type' which can have values 'Col1', 'Col2', 'Col3','Col4'
depending if its value is 'Y'

I'm using below code :

Select 
CASE Col1 WHEN 'Y' THEN 'Col1'
Else
CASE Col2 WHEN 'Y' THEN 'Col2'
Else
CASE Col3 WHEN 'Y' THEN 'Col3'
Else
CASE Col4 WHEN 'Y' THEN 'Col4'
End As Type

From table1

But not sure if its correct , got synatx errors.

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2008-05-12 : 04:33:46
Perhaps you're trying to do this?
Select 
CASE
WHEN Col1 = 'Y' THEN 'Col1'
WHEN Col2 = 'Y' THEN 'Col2'
WHEN Col3 = 'Y' THEN 'Col3'
WHEN Col4 = 'Y' THEN 'Col4'
END As Type
FROM table1


Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

vikashsql
Starting Member

6 Posts

Posted - 2008-05-12 : 04:43:12
Thanks Randell!!
I got the result , i had tried it earlier but missed END then and got confused :(

Thanks for the prompt reply!
Go to Top of Page
   

- Advertisement -