Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DB agnostic SQL for CURRENT_TIMESTAMP

I'm currently using Oracle, but most of the SQL is db agnostic. One exception is CURRENT_TIMESTAMP which fails in MSSQL. Is is possible to move to a more portable syntax for this too?

like image 622
qwerty Avatar asked Dec 12 '13 10:12

qwerty


People also ask

What does CURRENT_TIMESTAMP return in SQL?

The CURRENT_TIMESTAMP function returns the current date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format. Tip: Also look at the GETDATE() function.

What is the difference between now () and CURRENT_TIMESTAMP?

CURRENT_TIMESTAMP and CURRENT_TIMESTAMP() are synonyms for NOW() . Returns the current time as a value in 'hh:mm:ss' or hhmmss format, depending on whether the function is used in string or numeric context. The value is expressed in the session time zone.

What is difference between Getdate and CURRENT_TIMESTAMP?

CURRENT_TIMESTAMP is an ANSI SQL function whereas GETDATE is the T-SQL version of that same function. One interesting thing to note however, is that CURRENT_TIMESTAMP is converted to GETDATE() when creating the object within SSMS. Both functions retrieve their value from the operating system in the same way.

What is the timezone of CURRENT_TIMESTAMP in SQL?

CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE . The time zone offset reflects the current local time of the SQL session. If you omit precision, then the default is 6.


1 Answers

The jOOQ user manual can be useful for these kinds of lookups, as well as the SQL translation website. From the manual:

-- Access
now()

-- Sybase ASE
current_bigdatetime()

-- MariaDB, MemSQL, MySQL, Snowflake
current_timestamp()

-- BigQuery, CockroachDB, CUBRID, Db2, Derby, EXASOL, Firebird, H2, HANA, HSQLDB,
-- Ignite, Ingres, Oracle, PostgreSQL, Redshift, SQLite, SQL Server, Teradata
-- Vertica
CURRENT_TIMESTAMP

-- Informix
CURRENT

-- Sybase SQL Anywhere
CURRENT TIMESTAMP

Disclaimer: I work for the company behind jOOQ.

like image 93
Lukas Eder Avatar answered Oct 21 '22 18:10

Lukas Eder