0

How can I compare date column foo with the other date column bar and update column foobar with the more recent date.

ID        foo                    bar                foobar
1     '2014-01-23'         '0000-00-00'           '0000-00-00'
2     '0000-00-00'         '2013-03-01'           '0000-00-00'
3     '2013-03-03          '2014-04-04'           '0000-00-00'
Dap
  • 2,309
  • 5
  • 32
  • 44

2 Answers2

2

this statement updates foobar based on the condition in the case statement. Try something like this

UPDATE table_name  
SET     foobar =  CASE  
                        WHEN foo < bar THEN foo 
                        ELSE bar
                  END 
Sam
  • 2,761
  • 3
  • 19
  • 30
2

I think you can also use GREATEST:

UPDATE table_name
   SET foobar = GREATEST(foo,bar);
Community
  • 1
  • 1
zerodiff
  • 1,690
  • 1
  • 18
  • 23
  • i actually ended up using this one, in the end. so i switched my vote. also think its cleaner – Dap Sep 26 '14 at 19:32