How to do "select current_timestamp" in hsqldb?
Asked Answered
G

5

20

Oracle:

select systimestamp from dual

MySQL:

select current_timestamp

SQL Server:

select current_timestamp

PostgreSQL:

select current_timestamp

The question is, how can I get the current timestamp in HSQLDB? I use version 1.8.0.10

Goldfarb answered 26/5, 2009 at 9:1 Comment(0)
T
4

You can write

select current_timestamp from tablename  

where tablename is a real table in your database.

The result of the query is only the current timestamp.

Thayer answered 26/5, 2009 at 9:10 Comment(4)
Thanks, but with hibernate I do session.createSQLQuery("select current_timestamp from users") and I get empty list for .list() and null for .uniqueResult(). The table exists. If I put a non existent table I correctly get java.sql.SQLException: Table not found in statement [select current_timestamp from qwerty]Goldfarb
is the table you are using empty? for some reason I did a couple of tests and seems that if the result of the select is an empty list, you don't have the timestamp, if the result of the select is a list of rows, you get as many timestamps as the number of rows..Thayer
I know this is an old question, but if you want to write generic code (e.g. in a reusable library), that does not depend on specific a specific database, you could try using something like: SELECT TOP 1 current_timestamp FROM INFORMATION_SCHEMA.SYSTEM_TABLESCustodial
If the Table could be empty you need to wrap the timestamp with max aggregation and coalesce: select coalesce(max(current_timestamp()), current_timestamp()) from tablenamePhilpott
B
28

In a select I use

 SELECT CURRENT_DATE AS today, CURRENT_TIME AS now FROM (VALUES(0))
Blindage answered 3/7, 2011 at 10:25 Comment(2)
I prefer this solution as it doesn't require me to have any tables (for a plain connection test).Vanny
I found this answer more useful. My goal was to just do a simple select statement to verify the syntax. Most SQL implementation allow that without a FROM clause. This was a great work around.Worden
H
11

@alexdown's answer is quite right -- under 1.8 you need a one-row relation to do this, like Oracle's DUAL or the InterBase/Firebird RDB$DATABASE table.

When you move to the 2.0 series, however, you'll be able to use the SQL-99 "VALUES constructor" without reliance on a one-row relation:

sql> VALUES (current_timestamp);
2010-04-22 15:22:40.997

If you need to rename the column from the vendor-specific defaults that VALUES picks, you can always employ a select: SELECT * FROM (VALUES (current_timestamp)) v(my_new_name)

Hizar answered 22/4, 2010 at 15:34 Comment(0)
P
5

You can use

CALL current_timestamp

to retrieve the current timestamp. According to a discussion on the HSQL mailing list this is much more efficient than doing a dummy select from INFORMATION_SCHEMA.SYSTEM_TABLES.

Preraphaelite answered 3/8, 2011 at 8:22 Comment(0)
T
4

You can write

select current_timestamp from tablename  

where tablename is a real table in your database.

The result of the query is only the current timestamp.

Thayer answered 26/5, 2009 at 9:10 Comment(4)
Thanks, but with hibernate I do session.createSQLQuery("select current_timestamp from users") and I get empty list for .list() and null for .uniqueResult(). The table exists. If I put a non existent table I correctly get java.sql.SQLException: Table not found in statement [select current_timestamp from qwerty]Goldfarb
is the table you are using empty? for some reason I did a couple of tests and seems that if the result of the select is an empty list, you don't have the timestamp, if the result of the select is a list of rows, you get as many timestamps as the number of rows..Thayer
I know this is an old question, but if you want to write generic code (e.g. in a reusable library), that does not depend on specific a specific database, you could try using something like: SELECT TOP 1 current_timestamp FROM INFORMATION_SCHEMA.SYSTEM_TABLESCustodial
If the Table could be empty you need to wrap the timestamp with max aggregation and coalesce: select coalesce(max(current_timestamp()), current_timestamp()) from tablenamePhilpott
L
4

With HSQLDB 2.1 and later you have all the options.

With the connection property hsqldb.syntax_ora, hsqldb.syntax_pgs, hsqldb.syntax_mss or hsqldb.syntax_mys=true you can use the forms supported by other databases. The equivalent SQL is SET DATABASE SQL SYNTAX ORA TRUE, and similar for other dialects.

The native, SQLStandard form, supported by HSQLDB in all modes is this:

VALUES (CURRENT_TIMESTAMP)
Lodhia answered 17/4, 2011 at 9:59 Comment(1)
It's actually SET DATABASE SQL SYNTAX ORA TRUE. You were missing the "SQL" @fredt.Aforethought

© 2022 - 2024 — McMap. All rights reserved.