1

I am working on a spring api where I am using postgres and saving a field of type timestamp with field name as created_date. Now I want to check whether this field is between two different timestamps.

Right now I am using this:

 select *
from your_table
where created_date >= '2020-06-22 19:10:25-07'
and created_date < '2020-06-23 19:10:25-07'

Is there a way I can use BETWEEN here for this operation?

rudeTool
  • 526
  • 1
  • 11
  • 25
  • Why don't you just check this in the business logic layer? – deadlock Jun 27 '21 at 06:25
  • I am using this to search for all the records that have date between this range,I am doing this in the Repository layer. – rudeTool Jun 27 '21 at 06:28
  • You do not want to use `between`. It includes both end points and that is probably not what you intend. Your current version is probably the right way to write the logic. – Gordon Linoff Jun 27 '21 at 11:44

1 Answers1

2

You can do it like this:

SELECT *
FROM  your_table
WHERE created_date BETWEEN '2020-06-22 19:10:25-07'
AND '2020-06-23 19:10:25-07'

If you want to do it using JDBC, check this answer and this answer.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • thanks but will it work if the second param in BETWEEN in empty string,will it return the record with condition matching with first param? first param=first timestamp, second param=second timestamp – rudeTool Jun 27 '21 at 06:38
  • `BETWEEN` requires two values. If you want to work with only the first value, you can use `SELECT * FROM your_table WHERE created_date >= '2020-06-22 19:10:25-07'`. – Arvind Kumar Avinash Jun 27 '21 at 06:46