1

I need to select some data set between a specific date range, the table has date and time in "23-JUL-18 04.03.02.584000000 PM" format (TIMESTAMP(3)).

What is the way to filter data between 23-JUL-18 04.00.00 and 23-JUL-18 04.03.02 using java.

edit: I need to pass the date range using prepared statement and I'm using Java 7

Thank you in advance!

5
  • can you show us your code? what you are using jdbc JPA... Commented Aug 1, 2018 at 14:44
  • 1
    By "using Java" so you mean that you need to pass the date range into the query as a parameter from Java (via setTimestamp(); or will they be fixed values in the query? Incidentally, timestamp columns have an internal representation, it's your client that formats that to something readable like the values you've shown. Commented Aug 1, 2018 at 14:47
  • @YCF_L "SELECT * FROM table WHERE createdtime BETWEEN ? AND ?"; & I'm using prepared statement Commented Aug 2, 2018 at 3:31
  • @Alex Yes I need to pass date range as prepared statement parameters, but when I do that using setTimestamp(), I got a sql exception (ORA-00932: inconsistent datatypes: expected TIMESTAMP got NUMBER) Commented Aug 2, 2018 at 3:35
  • Then really you should have included the code you were using and the error you got in your question. Doesn't really matter now as Tim's answer shows you what to do, but bear in mind for future questions. Commented Aug 2, 2018 at 7:15

1 Answer 1

3

If you are calling Oracle from Java via JDBC, then the best thing to do would probably be to use a prepared statement:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String lower = "2018-07-23 04:00:00";
String upper = "2018-07-23 04:03:02";
LocalDateTime lowerdt = LocalDateTime.parse(lower, formatter);
LocalDateTime upperdt = LocalDateTime.parse(upper, formatter);

String sql = "SELECT * FROM yourTable WHERE ts_col BETWEEN ? AND ?;";
Statement statement = conn.createStatement(sql);
statement.setTimestamp(1, Timestamp.valueOf(lowerdt));
statement.setTimestamp(2, Timestamp.valueOf(upperdt));
ResultSet rs = statement.executeQuery();
while (rs.next()) {
    // process result set
}

This is the Java 8+ way of using a prepared statement to run a select with timestamps against Oracle. Not much needs explaining, except that to form the BETWEEN clause, we start out with just fairly standard string timestamps for the two points. Then, we parse into LocalDateTime, and finally bring those into java.sql.Timestamp.

Here is a link to a helpful GitHub page which shows JDBC with Java 8 and Oracle being used in a variety of different ways.

Edit: Here is a sample of how you might do the same as the above in Java 7:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String lower = "2018-07-23 04:00:00";
String upper = "2018-07-23 04:03:02";
Date lowerdt = formatter.parse(lower);
Date upperdt = formatter.parse(upper);
String sql = "SELECT * FROM yourTable WHERE ts_col BETWEEN ? AND ?;";
Statement statement = conn.createStatement(sql);
statement.setTimestamp(1, new java.sql.Timestamp(lowerdt.getTime()));
statement.setTimestamp(2, new java.sql.Timestamp(upperdt.getTime()));
ResultSet rs = statement.executeQuery();
while (rs.next()) {
    // process result set
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick answer! but I forgot to mention that I'm using Java 7 and cannot use above code example in my project. Do you know a way to accomplish the same in Java 7
I was going to edit my answer, but I think most future readers will have already moved past Java 7. Here is a tutorial which will show you how to do the same in Java 7.
Thanks for the answer Tim!
@Sameera.San If you get genuinely stuck trying to write the Java 7 version, I can post something. But, I think you can figure it out between my answer and the tutorial.
I solved my problem but it would be great if you can post the answer because someone will need it.

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.