SQL Date Manipulation
The DATE datatype in Oracle RDBMS stores dates in Julian format internally. This means that the maximum valid date in a DATE column would be 31-Dec-4712.
Oracle
MySQL
MS-SQL
Postgres
SYSDATE
TRUNC
SYSDATE()
DATE
DATE_FORMAT
SUBDATE
CURRENT_TIMESTAMP
DATEADD
DATEDIFF
DATEFROMPARTS
DATENAME
DATEPART
DAY
GETDATE
GETUTCDATE
ISDATE
MONTH
SYSDATETIME
YEAR
Examples
Truncate to Day
Expected Result:
2023-03-03
Oracle
ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD';
To override the default date display formatSELECT TRUNC(SYSDATE) FROM dual;
SELECT TRUNC(SYSDATE, 'DD') FROM dual;
MySQL
SELECT CURRENT_DATE;
SELECT DATE(SYSDATE());
First Day of Month
Expected Result:
2023-03-01
Oracle
ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD';
To override the default date display formatSELECT TRUNC(SYSDATE, 'MM') FROM dual;
MySQL
SELECT DATE_FORMAT(CURRENT_DATE,'%Y-%m-01');
SELECT DATE_FORMAT(SYSDATE(),'%Y-%m-01');
Yesterday
Expected Result:
2023-03-02
MySQL
SELECT DATE_FORMAT(SUBDATE(CURRENT_DATE, 1),'%Y-%m-01');
SELECT DATE_FORMAT(SUBDATE(SYSDATE(), 1),'%Y-%m-01');
UNIXTIME (Epoch)
PostgreSQL, where Epoch is stored in a "character varying" datatype called epoch_value...
SELECT epoch_value AS epoch,
TO_TIMESTAMP(CAST(epoch_value AS BIGINT)/1000) AS datetime
FROM mytable;
Bibliography
Oraclehttps://www.sqlines.com/oracle-to-mysql/trunc_datetime
MySQLhttps://www.sqlines.com/oracle-to-mysql/trunc_datetimehttps://stackoverflow.com/questions/7146828/mysql-selecting-yesterdays-date
PostgreSQLhttps://stackoverflow.com/questions/24751372/no-function-matches-the-given-name-and-argument-typeshttps://stackoverflow.com/questions/16609722/postgresql-how-to-convert-from-unix-epoch-to-datehttps://www.postgresqltutorial.com/postgresql-string-functions/postgresql-to_char/