How to get current DB time from SQL Server using spring boot - java

I am using spring boot and SQL Server and getting unexpected end of subtree when I have the following code to get database 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();

ok I fixed it by doing:
#Query(value = "SELECT CURRENT_TIMESTAMP", nativeQuery = true)
public Timestamp getTime();

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

JPA query does not return any data

I have a following JPA query:
#Query(value = "select r FROM TableEntity r where r.time=?1")
TableEntity findByTime(java.sql.Timestamp time);
That query is pretty straightforward, should fetch the database row based on some time. It works like a charm with MYSQL and Oracle but won't work for MSSQL. I have debugged the query through Hibernate and JTDS driver and saw that Timestamp is successfully resolved in the Prepared statement, I can see the exact number of hours, minutes, seconds and milliseconds in query as I have in the database row. However, no data is returned back.
class TableEntity {
#Type(type = "timestamp")
private Timestamp time;
}
I am suspecting that some milliseconds rounding happens or they somehow gets messed up since query does return something once in a blue moon!
My Time Field in the database is datetime2(3)
I am using the net.sourceforge.jtds 1.3 driver.
Time is formatted like this: 2020-06-03 13:02:21.273, I am working with milliseconds
EDIT:
I tried writing plain prepared statement and here are results:
select r FROM TableEntity r where r.time=?1
preparedStatement.setTimestamp(1, timestamp); //does not work...
preparedStatement.setString(1, timestamp.toString()) //works like a charm
Any idea?
So, under the hood, Hibernate was mapping that timestamp to datetime type when it queried the DB (I concluded that using the MSSQL profiler) . Since I had datetime2 in the DB, comparing datetime with datetime2 on the DB level was not returning anything even if they were exactly the same in Milliseconds.
Solution would be to force mapping of timestamp to datetime2 so db datetime2 type gets queried with the same type

TIMESTAMP (6) WITH TIME ZONE to Time stamp

I have a query in my Java class that fetches (Order_num) VARCHAR and Time_Field(TIMESTAMP (6) WITH TIME ZONE) as shown below:
select order_num,time_Field from
MY_TABLE where ORD_NUM='123456789' ORDER BY time_Field desc
However it gives me "
No Dialect mapping for JDBC type: -101" exception
that I highly doubt is due to the mapping between Oracle database and Hibernate. Because without the "Time_Field" being returned the query runs fine.
Is there a way I can cast or convert the "TIMESTAMP WITH TIME ZONE" to simple "TIMESTAMP"?
I finally figured it out by running the below query:
SELECT order_num,TO_CHAR(
FROM_TZ( CAST( time_Field AS TIMESTAMP ), 'UTC' )
AT LOCAL,
'YYYY-MM-DD HH:MI:SS PM'
) AS local_time
FROM MY_TABLE
where ord_num='123456789'
ORDER BY time_Field desc;

How to use to_char function in JPA to get records from a timestamp

I am try to query from and database using eclipse link using entities. when I use normal query records are returned from current date by when I use JPA zero records are returned. Where could I be doing wrong, Or how can I use to_char function in JPA my database is postgresql.
normal sql query that returns records
select *
from mytable
where to_char(transaction_date, 'YYYY-MM-DD') = '2016-01-19';
MyEntity
#Temporal(javax.persistence.TemporalType.DATE)
private Date transaction_date;
EntityManager e = EntityMgr.MyFact().createEntityManager();
e.getTransaction().begin();
Query qry = e.createQuery("from MyEntity u where u.transaction_date=?1");
qry.setParameter(1, new Date(), TemporalType.DATE);
Have you tried with qry.setParameter(1, new Date(), TemporalType.TIMESTAMP);
You can use to_char function using operator
like
OPERATOR('ToChar', transaction_date,'YYYY-MM-DD')= '2016-01-19';

SQL SELECT and timestamp columns

In my application i have a java Timestamp dateCreated, which is inserted to a mysql Timestamp colum. Inserting is no problem, i use a prepared statement and statement.setTimestamp(dateCreated).
Now i need to select a row with the dateCreated as unique identifier. my method gets another java Timestamp object.how does the SQL query work in this case? i havent figured out how to compare the java timestamp to the mysql one.
SELECT * FROM table WHERE timestamp_column = ???
Thanks!
In Java you will use a similar setTimestamp method as you did with the insert.
Timestamp t = ???;
String sql = "SELECT * FROM table WHERE timestamp_column = ?";
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setTimestamp(t);
etc.

How to obtain CURDATE() / NOW() on a JPA named query?

I want to do a select from table where date = TODAY, on mysql that would be where date > CURDATE(), how do I do this on a JPA named query?
From the spec:
4.6.16.3 Datetime Functions
functions_returning_datetime:=
CURRENT_DATE |
CURRENT_TIME |
CURRENT_TIMESTAMP
The datetime functions return the
value of current date, time, and
timestamp on the database server.
That depends on your JPA provider. Hibernate, for example, supports current_date function:
from MyEntity where myDateProperty > current_date

Categories