Formatting Dates with the TO_CHAR Function

Moderator: NorbertKrupa

Post Reply
User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Formatting Dates with the TO_CHAR Function

Post by JimKnicely » Fri Aug 10, 2012 12:25 pm

If you’ve been using MySQL and SQL Server then you are probably familiar with the DATE_FORMAT (MySQL) and CONVERT (SQL Server) functions when you need to format a date string.

Vertica, like Oracle, has a function named TO_CHAR that can be used to format dates.

Examples:

Code: Select all

cognos=> SELECT current_timestamp                         "RIGHT NOW"
cognos->      , to_char(current_timestamp, 'MM/DD/YYYY')  "MM/DD/YYYY"
cognos->      , to_char(current_timestamp, 'Mon-YYYY')    "Mon-YYYY"
cognos->      , to_char(current_timestamp, 'YYYYMMDD')    "YYYYMMDD"
cognos->      , to_char(current_timestamp, 'HH:MI:SS AM') "HH:MI:SS AM/PM"
cognos->      , to_char(current_timestamp, 'HH24:MI:SS')  "HH24:MI:SS";
           RIGHT NOW           | MM/DD/YYYY | Mon-YYYY | YYYYMMDD | HH:MI:SS AM/PM | HH24:MI:SS
-------------------------------+------------+----------+----------+----------------+------------
2012-05-30 15:26:25.466157-04 | 05/30/2012 | May-2012 | 20120530 | 03:26:25 PM    | 15:26:25
(1 row)
Have fun!
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

Post Reply

Return to “Vertica Tips, Lessons and Examples”