Dan Goodspeed
Dan Goodspeed

Reputation: 3560

Choose different column on SELECT if one is empty

I have a table with two columns (well, two columns that concern this question) - pub_date and update_date. pub_date always has a date, and update_date is empty unless the row was updated. Is there any way to use a SELECT call to return update_date, but fall back to pub_date if update_date is empty?

Upvotes: 1

Views: 84

Answers (2)

Pரதீப்
Pரதீப்

Reputation: 93694

Use coalesce function

The COALESCE function returns the first of its arguments that is not null. Null is returned only if all arguments are null

select coalesce(update_date,pub_date),update_date,pub_date
from yourtable 

Upvotes: 4

Rigel1121
Rigel1121

Reputation: 2016

Use CASE Statement:

SELECT 
      CASE WHEN 
               update_date IS NULL OR 
               update_date='' 
           THEN pub_date ELSE update_date 
      END AS update_date 
FROM YourTable

Upvotes: 0

Related Questions