Be able to set the display format of date/time data in relational database management systems.
DATE, TIME, DATETIME, TIMESTAMP, INTER \cdot AL.| Database | Function | Example | Result |
|---|---|---|---|
| MySQL | DATE_FORMAT(date, format) |
DATE_FORMAT(order_date, '%d-%m-%Y') |
23-04-2024 |
| PostgreSQL | TO_CHAR(date, format) |
TO_CHAR(order_date, 'DD.MM.YYYY') |
23.04.2024 |
| Oracle | TO_CHAR(date, format) |
TO_CHAR(order_date, 'DD-MON-YYYY') |
23-APR-2024 |
| SQL Server | FORMAT(date, format) |
FORMAT(order_date, 'dd/MM/yyyy') |
23/04/2024 |
%d – Day of month (01–31) – MySQLDD – Day of month (01–31) – PostgreSQL/Oracle%m – Month (01–12) – MySQLMM – Month (01–12) – PostgreSQL/Oracle%Y – Year (four digits) – MySQLYYYY – Year (four digits) – PostgreSQL/OracleHH – Hour (01–12) – PostgreSQL/Oraclehh – Hour (01–12) – MySQLHH24 – Hour (00–23) – PostgreSQL/Oraclehh24 – Hour (00–23) – MySQLMI – Minutes – PostgreSQL/Oracle%i – Minutes – MySQLSS – Seconds – PostgreSQL/Oracle%s – Seconds – MySQLSELECT statement to present the data.Suppose you have a table orders with a column order_date of type DATETIME. You want the report to show dates as DD/MM/YYYY in MySQL:
SELECT order_id,
DATE_FORMAT(order_date, '%d/%m/%Y') AS formatted_date
FROM orders
ORDER BY order_date DESC;
When working with TIMESTAMP data that stores UTC, you may need to convert it to the local time zone before formatting:
CON \cdot ERT_TZ(timestamp_col, 'UTC', 'America/New_York')timestamp_col AT TIME ZONE 'UTC' AT TIME ZONE 'America/New_York'FROM_TZ(CAST(timestamp_col AS TIMESTAMP), 'UTC') AT TIME ZONE 'America/New_York'AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time'YYYY-MM-DD.TIMESTAMP column as Month DD, YYYY HH24:MI?DATE_FORMAT and TO_CHAR in terms of syntax and supported specifiers.TIMESTAMP to British Summer Time (BST) and format it as DD-MMM-YYYY HH24:MI in Oracle.