问题
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.
回答1:
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.
回答2:
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()
回答3:
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.
来源:https://stackoverflow.com/questions/13884830/how-do-i-get-current-date-in-mssql