Page is a not externally linkable
- Code, Content, and Presentation
-- Databases
---- having problem with IF ELSE to display content


Demaestro - 4:08 pm on Jul 12, 2010 (gmt 0)


hoom,

Depending on your data there are a few ways to do this.

If the image field is null then you can use coalesce.

select coalesce(img_field, '/images/placeholder') as img_field from table

If the image field is an empty string then you may want to use a case/switch statement or an if.

select
case img_field
when (img_field == '') then '/images/placeholder'
else img_field
end
from table


or

select
if (img_field=='','/images/placeholder', img_field)
from table

My syntax might be a little off, I am going from memory. but if you search for "sql case" or "sql coalesce" or "sql if else" you will find lots of good examples.


Thread source:: http://www.webmasterworld.com/databases_sql_mysql/4168600.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com