2

I have a problem similar to this question but with an added complication: MySql, combining date and time column into a time stamp

I have two columns one for date and one for time and I need to combine them as a timestamp for comparison. Setting a time is optional in our application and in some cases it's null. If I use CONCAT it only works for the instances where the time is set.

Is the best way to do this, have two queries with a UNION?

Thank you!

Community
  • 1
  • 1
Jonny White
  • 875
  • 10
  • 21

2 Answers2

5

Use a COALESCE to choose a default time value when yours is NULL.

CONCAT(datefield, ' ', COALESCE(timefield, '00:00:00')) AS date
Joe Stefanelli
  • 132,803
  • 19
  • 237
  • 235
0
concat(datefield,' ',ifnull(timefield,'00:00:00')) as date
Brian Hoover
  • 7,861
  • 2
  • 28
  • 41