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
 General SQL Server Forums
 New to SQL Server Programming
 Can I If Statement on Select

Author  Topic 

Mauricio Moreno
Starting Member

18 Posts

Posted - 2007-08-03 : 16:39:18
hello,

I am not sure if this is possible but i need to know if this is something that is possible. On a select statement, can i tell do an if statement for a record. Ill give you an example of what i mean. I dont know how to constuct this in sql, so ill just do what i mean in asp.

select Id, Desc, If image1 <> "" then image = image1 else image = image2, price from products

The results are not going into just an excel sheet, so i can't do that bit of asp code after the recordset is output.

Any help and or enlightement on this would be greatly appreciated...

~ Moe

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-08-03 : 16:40:38
A Case Expression will do what you need.

select id, desc, case when image1 <> '' then image1 else image2 end, price from products

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2007-08-03 : 16:41:52
[code]
SELECT
ID,
Desc,
Image = CASE When (Image1 IS NULL OR Len(Image1) = 0) Then Image2 ELSE Image1 END,
Price
FROM
Products
[/code]

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2007-08-04 : 06:16:46
case when nullif(image1,'') is null then image2 else image1 end


--------------------
keeping it simple...
Go to Top of Page
   

- Advertisement -