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
 Transact-SQL (2000)
 IF statement within SELECT (creating a view)

Author  Topic 

theLastFlop
Starting Member

4 Posts

Posted - 2006-08-27 : 09:14:28
I keep getting syntax errors..

I have a table with the following layout:
Col01 Col02 Col03 Col04
A 1 B 6
B 5 A 7

The value 'A' can show up in either Col01 or Col03. I am trying to create a field in a view where it finds which Column 'A' shows up in, and then returns a value. So if Col01 = 'A', then give me the value in Col02....so on

this is what I have so far....
SELECT
(IF (table.Col01 = 'A') SELECT Col02 FROM table ELSE SELECT Col04 FROM table) as AValue

I am new to SQL and am used to writing in VBA so this is probably contributing to my confusion. HELP!!

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-27 : 09:17:40
use case when

select
case when Col01 = 'A' then Col02 else Col04 end as AValue
from table



KH

Go to Top of Page
   

- Advertisement -