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

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

Related

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

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();

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;

Generic Format DateTime for MySQL and Oracle

I want to cast a string to UTC date. But with environments, database varies and code needs to be changed accordingly as below.
if env1
//mysql
insert into table values (STR_TO_DATE('datetime','%%m/%%d/%%Y %%H:%%i:%%s'))
else
//oracle
insert into table values (to_date('%s', 'MM/DD/YYYY HH24:MI:SS'))
So, Is there a way to handle this generic? by just generating UTC date in code itself and then inserting in database accordingly with any date exception in database?
PreparedStatement has three methods to use to set dates: setDate, setTime and setTimestamp.
You can use either of them that suits you best.
To get the PreparedStatement object, call .prepareStatement("your sql query") on your connection ojbect.
In your case, your query will be "insert into table values (?)"
Within the databases, you can use the same INSERT statement if you use a TIMESTAMP literal (MySQL documentation, Oracle documentation):
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( value DATE );
INSERT INTO table_name ( value ) VALUES ( TIMESTAMP '2018-03-23 11:12:00' );
Query 1:
SELECT * FROM table_name
Results:
| VALUE |
|----------------------|
| 2018-03-23T11:12:00Z |
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE table_name ( value TIMESTAMP );
INSERT INTO table_name ( value ) VALUES ( TIMESTAMP '2018-03-23 11:12:00' );
Query 1:
SELECT * FROM table_name
Results:
| value |
|----------------------|
| 2018-03-23T11:12:00Z |

MySQL check case where a date is 0000-00-00 00:00:00?

I have this MySQL query:
SELECT `date_joined`, `date_last_joined` FROM `users` WHERE `name` = "mary"
The date_last_joined column is currently 0000-00-00 00:00:00 for all users. It's then updated to the current date next time they log in.
So my question is, how would I return the date_joined if date_last_joined is 0000-00-00 00:00:00, and date_last_joined if it isn't? Is this possible in a MySQL query?
The reasons for this are that returning 0000-00-00 00:00:00 in a Java prepared MySQL query causes all sorts of issues.
You can use the IF flow control operator:
SELECT IF(`users`.`date_last_joined` = '0000-00-00 00:00:00',
`users`.`date_joined`, `users`.`date_last_joined`) AS `join_date`
FROM `users`
WHERE `users`.`name` = 'mary'

How do I get CURRENT_DATE in MSSQL?

I am using jpa 3.o with Hibernate. I have one named query:
SELECT COUNT(wt.id) FROM WPSTransaction wt WHERE wt.createdDate>= CURRENT_DATE
WPSTransaction is my entity class and createdDate is one of the columns in my class.
It's working fine in the Mysql Database. However, I'm moving to SQL Server 2012 and SQL server doesn't seem to compile the CURRENT_DATE value. I've tried GETNOW() and NOW() methods as well as current_date() method and CURRENT_TIMESTAMP without any luck.
MS SQL Server fails to conform to the standard here - it does not provide a current_date keyword.
You must instead use the SQL-server specific GETDATE() function, as the document linked shows.
Because current_date is a keyword in the spec, you can't just create a user-defined current_date() function in MS SQL and expect it to work the same. So unfortunately you're stuck with database-specific code here.
The function to return the current date in MS SQL is GETDATE(), so your query should read
SELECT COUNT(wt.id) FROM WPSTransaction wt WHERE wt.createdDate >= GETDATE()
How about:
Query q = entityManager.createQuery("SELECT COUNT(wt.id) FROM WPSTransaction wt WHERE wt.createdDate>= :d");
q.setParam("d", new Date());
No database specific code needed.

Categories