What is a better way to program the following SQL:
str_to_date(
concat(convert(D.DAY, CHAR(2)),
'-',
convert(M.MONTH, CHAR(2)),
'-',
convert(M.YEAR, CHAR(4))),
'%e-%m-%Y' ) as AMOUNT_DATE
I want to convert the columns D.DAY, M.MONTH, and M.YEAR to a date field using MySQL. The above works, but seems much more complicated than it needs to be. (If there’s an ANSI SQL way to do it, that would be even better.)
Thank you!
One small improvement you can make is using
CONCAT_WSinstead ofCONCAT(the “WS” stands for “with separator”–it’s the equivalent ofjoinorimplodein other languages). And MySQL should automatically convert the arguments to strings, so theCONVERTfunctions are, I believe, unnecessary.I’m guessing this doesn’t perform as well as Tomalak’s arithmetic solution, though.