0

My code looks like:

Session session = sessionFactory.getCurrentSession();
        Query countQuery = session.
                createSQLQuery(queryString);
        Long totalFilteredRecords = ((Number)countQuery.uniqueResult())
                .longValue();

The queryString is:

SELECT COUNT(id)
FROM SOME_TABLE
WHERE
  to_char(LAST_UPDATED_DATE, 'yyyy-mm-dd hh:mi:ss')
BETWEEN
  '2015-11-13 11:40:03'
AND
  '2015-11-13 11:34:03'

LAST_UPDATED_DATE is Date type.

I run this SQL on SQLDeveloper and it works well, but failed when run via Hibernate:

org.hibernate.util.JDBCExceptionReporter: SQL Error: -3407, SQLState: 22007
org.hibernate.util.JDBCExceptionReporter: data exception: invalid datetime format: yyyy-mm-dd hh:mi:ss

My database is Oracle.

Could anybody help clarify is there anything wrong?

5
  • 1
    @Siyual No, it's correct oracle format. mm is for months, mi - for minutes. Commented Nov 13, 2015 at 19:56
  • Can you make sure, that your date is in correct format? Try to log values of LAST_UPDATED_DATE somewhere. For example, in log table. Commented Nov 13, 2015 at 19:58
  • Also, try hh24:mi:ss instead of hh:mi:ss Commented Nov 13, 2015 at 20:00
  • Oh, wait! LAST_UPDATED_DATE is a column with date type? So write directly LAST_UPDATED_DATE between ... and ... Commented Nov 13, 2015 at 20:03
  • @Dmitry I replace date format string from lower case to upper case and it works... Commented Nov 16, 2015 at 21:09

3 Answers 3

1

it's not need to cast the date value to string and compare it, you could also use parameter for the hql query:

Query q = em.createQuery("SELECT COUNT(id) "+
     "FROM SOME_TABLE "+
     "WHERE "+
     "  LAST_UPDATED_DATE >= :startDate AND LAST_UPDATED_DATE <=:endDate";
q.setParameter("startDate",your_start_date,TemporalType.DATE);
q.setParameter("endDate",your_end_date,TemporalType.DATE);
Sign up to request clarification or add additional context in comments.

1 Comment

This is good way! But due to some reason I'm not able to change the code. Thus the only way is to pass a pure SQL query...
1

Typical constrain of a DATE column is as follows

 SELECT COUNT(id)
 FROM SOME_TABLE
 WHERE
    LAST_UPDATED_DATE  /* Column with DATE format */
 BETWEEN
   /* DATE literal using explicit formatting */
   to_date('2015-11-13 11:40:03', 'yyyy-mm-dd hh24:mi:ss')
 AND
   to_date('2015-11-13 11:34:03', 'yyyy-mm-dd hh24:mi:ss')

1 Comment

Yes another good way, if replace yyyy-mm-dd hh24:mi:ss to upper case, then it will work.
1

Found the answer, it is a little unexpected simple... Replace lower case date format string to upper case.

Change queryString to:

SELECT COUNT(id)
FROM SOME_TABLE
WHERE
  to_char(LAST_UPDATED_DATE, 'YYYY-MM-DD HH24:MI:SS')
BETWEEN
  '2015-11-13 11:40:03'
AND
  '2015-11-13 11:34:03'

And it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.