Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date to string

Tags:

sql

db2

I'm trying to format a db2 date into a string as "YYYY/MM/DD".

The best I got so far is:

SELECT CAST(YEAR(MYDATE) AS VARCHAR(4)) || '/'
|| CAST(MONTH(MYDATE) AS VARCHAR(2))    || '/'
|| RIGHT('00' || CAST(DAY(MYDATE) AS VARCHAR(2)), 2) FROM MYCALENDAR

Is there a better, terser way to do this?

ps: Toying around with locales is not an option.

like image 357
BlakBat Avatar asked Dec 10 '22 03:12

BlakBat


1 Answers

According to the IBM documentation the following should work:

 SELECT VARCHAR_FORMAT(MYDATE, 'YYYY/MM/DD') FROM MYCALENDAR;
like image 165
Dennis Traub Avatar answered Dec 31 '22 03:12

Dennis Traub