DB2 TO_CHAR Function: Usage & Examples

The TO_CHAR function in DB2 is used to convert date, time, or numeric values into character format. Its syntax is as follows:

TO_CHAR(expression, format)

In this case, the expression is the value to be converted and the format is the conversion format. For example, converting a date to a character format:

SELECT TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD') FROM SYSIBM.SYSDUMMY1;

This will return the character format of the current date, for example, 2021-10-12. Another commonly used format is YYYY-MM-DD HH24:MI:SS for date and time formatting.

bannerAds