Home > Software engineering >  How to get current DB time from sql server using spring boot
How to get current DB time from sql server using spring boot

Time:09-24

I am using spring boot and sql server and getting unexpected end of subtree when I have the following code to get DB time;

@Query("SELECT CURRENT_TIMESTAMP")
public Timestamp getTime();

SELECT CURRENT_TIMESTAMP works when ran in sql server though.

and when I do the following, I get the timestamp but in a list, based on how many rows the table has:

@Query("SELECT CURRENT_TIMESTAMP from MYTABLE")
public Timestamp getTime();

EDIT:

ok I fixed it by doing:

@Query(value = "SELECT CURRENT_TIMESTAMP", nativeQuery = true)
public Timestamp getTime();

CodePudding user response:

ok I fixed it by doing:

@Query(value = "SELECT CURRENT_TIMESTAMP", nativeQuery = true)
public Timestamp getTime();

CodePudding user response:

The Java Persistence query language includes the built-in Datetime Functions, which may be used in the WHERE or HAVING clause of a query. Such as:-

functions_returning_datetime:=
             CURRENT_DATE |
             CURRENT_TIME |
             CURRENT_TIMESTAMP

This way you can get it by:-

@Query(value = "SELECT CURRENT_TIMESTAMP", nativeQuery = true)
public Timestamp getTime();
  • Related