Hibernate JPQL CURRENT_DATE not working on SQL Server - sql-server

I have the following JPQL query:
SELECT x FROM Bla x WHERE x.deadline < CURRENT_DATE
This works as expected on MySQL. But SQL Server complains that CURRENT_DATE is not recognized.
I am asking myself what exactly the problem is. Then CURRENT_DATE is a standard JPA function that should resolve independent from the underlying RDBMS.
Further the Hibernate documentation also has CURRENT_DATE documented (see here: http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch11.html#ql-expressions).
I googled and found a lot fo comments telling me that I shall use CURRENT_DATE() instead. But this isn't JPQL.
So why is CURRENT_DATE not working and what is the solution?
I am using Hibernate 4.1.8.Final.
Best regards,
Florian

You can try to pass current date as a parameter to the query. Also, there will no portability issue whith this.
session.createQuery("SELECT x FROM TABLE_NAME x WHERE x.deadline < :currentDate")
.setParameter( "currentDate", currentDate)
.list();
Else, you can try query with current_date() instead of current_date, which is part of HQL expression.

try using GETDATE()
SELECT x FROM Bla x WHERE x.deadline < GETDATE()

Related

How to insert current_timestamp using Spring JDBCNamedParameterTemplate in MS SQL Server?

I am using a prepared statement using Spring JDBCNamedParameterTemplate in MS SQL Server
String sqlUpsateEntity = "update mytable \n" + "set name = :myname, Current_Date = current_timestamp from mytable where mytable_id = :id";
"Current_Date = current_timestamp " gives error
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'Current_Date'."
How can I insert the current timestamp?
Update
#Martin Cairney pointed out the cause may be another issue and he is right.
When I added the 'Current_Date = current_timestamp' in the actual code, I had
forgotten to put a ',' after the previous clause. So current_timestamp works just fine.
The syntax looks to be OK - I am not convinced that the use of CURRENT_TIMESTAMP is your issue, but possible ways to check this include using the other system date and time options:
SYSDATETIME()
GETDATE()
I'd suggest trying with each of those to see if it does the same.
Another option, without seeing your table schema and parameter values is that there is a mis-match between those. Check that you are passing the appropriate data type for :myname and also that Current_Date is a DATETIME data type and also that the value passed for :id matches the data type of column mytable_id.

how to get the current year from postgres sql query

how to get the current year from postgresql query, I tried like normal mysql query .But this is not working in postgresql?
SELECT YEAR(CURDATE());
try :
select to_char(now(),'YYYY')
you could also do this
select date_part('year', CURRENT_DATE);
In standard ANSI SQL (which is also supported by Postgres) this would be:
extract(year from current_date)

How to convert GetDate() to "YYYY-MM-DD-HHMM" format?

I've been looking for the answer and could not find the exact one satisfying my needs.
I'm looking for the way to convert Datetime value into YYYY-MM-DD-HHMM format.
I tried that one:
select CONVERT(varchar(20),GETDATE(), 120) + replace(convert(varchar(5),getdate(),108),':','')
But it did not give me the right results
Is there any practical way to do it?
In SQL Server 2012 or later you can use the built-in Format() function for this:
Select Format(GetDate(), N'yyyy-MM-dd-HHmm')
This works for me:
select CONVERT(varchar(10),GETDATE(), 20) + '-' + replace(convert(varchar(5),getdate(),108),':','')
I'm not sure if this is efficient or not, though

How to see HQL final query

I'm trying to run the following query and getting a null pointer exception. I've verified that WHERE lockedTimestamp <= (GETDATE() - CAST('00:06' AS datetime) is valid msSQL because I can run it manually and it'll work fine. It appears HQL (Hibernate Query Language) may not like it. Any thoughts on how I can go about debugging this?
Query query = this.em.createQuery("SELECT c FROM Content c WHERE lockedTimestamp <= (GETDATE() - CAST('00:06' AS datetime))");
Why not use a much more obvious and intuitive calculation, e.g.
SELECT c FROM Content c WHERE lockedTimestamp <= DATEADD(MINUTE, -6, GETDATE());
? I gather this isn't valid in JPQL either, but more for the general query itself in case other users stumble across this question...
As an aside, why not put this query in a stored procedure? Surely hibernate can call a stored procedure, then it doesn't need to try to interpret or reverse engineer your query.
EntityManager.createQuery expects a query string in JPQL (not HQL, and not SQL). JPQL does not define a CAST function.
I'd compute the timestamp limit in Java, and pass it as parameter to the query:
Date limit = ...;
this.em.createQuery("SELECT c FROM Content c WHERE lockedTimestamp <= ?")
.setParameter(0, limit);
To debug sql, you can show the queries that launches Hibernate by adding the configuration hibernate.show_sql=true in the hibernate configuration properties.
The queries will be shown in console, and it's the exact query that hibernate make to the database.

Query Oracle DB for timestamps when you know milliseconds

I'm currently getting the error:
java.sql.SQLException: ORA-01843: not a valid month
which I assume is to do with the way I am setting a timestamp...
So I have a query like this :
select * from A_TABLE where A_TIMESTAMP_COL < '1252944840000'
But it doesn't work...and I don't want to have to convert it to a date ideally. Is there some special syntax to tell Oracle that this is a timestamp?
You can use the to_timestamp() function to convert your string into a timestamp value: http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions201.htm#sthref2458
I think you can cast the date column to a char and do something like:
select * from A_TABLE where to_char(A_TIMESTAMP_COL) < '1252944840000'
This should allow you to compare strings, not dates.
Use the java.sql convenience methods to take that milisecond time and turn it into a Date or Timestamp.
You can use this query:
SELECT * FROM A_TABLE WHERE TIMESTAMP < (SYSDATE - 10/1440)
Where (SYSDATE - 10/1440) means SYSDATE - 10 Minutes.
Also see some examples here.

Resources