0

I have a PSQL database that has fields for "start_time" and "end_time" in a table.

These get imported from a json file as string to a text field in the database. The string has the following format: yyyy-mm-dd hh:mm:ss I want to do a query to give all id's of a table in the database that are within a given date range.

To do this I can do one of two things. Either I can query for a list of start and end times given a date range, and with the return from that query do a separate query for the actual data I am looking to get with WHERE start_time = ANY ('{list of dates go here}')

The other option is to figure out how to change the format of the row to something that is comparable. Something like WHERE start_time > beginning of date range AND end_time > end of date range if the fields are not text fields.

The date range is going to come from a date time picker from a website.

This might be a duplicate post but after hours of searching for a solution to either of those nothing has come up. So anything in the right direction is helpful.

Thanks

3
  • I guess I'm not sure what your question is, here. Are you asking whether it's better to store timestamps as timestamps, or as text? Or are you asking how to convert your text-timestamps to timestamp-timestamps? Commented Aug 18, 2016 at 18:07
  • Don't add tags for things that aren't relevant. This is Postgres, not MySQL. Commented Aug 18, 2016 at 19:24
  • Added tags and background information in case there was a better solution to process the information on either the server side in python or on the client side in the javascript before or after the dates were put into a query. However postgres is magic and Boris's answer is exactly what I am looking for. Commented Aug 18, 2016 at 19:52

1 Answer 1

2

PostgreSQL should go easy on you in this case. Simply cast the string to timestamp and to the comparison.

This should work out of the box:

WHERE start_time::timestamp > '2016-01-01' AND end_time::timestamp < '2016-12-31'
Sign up to request clarification or add additional context in comments.

4 Comments

Fair answer. Just two corrections. 1. second comparison should be < not >. 2. For performance on big dataset, you might need expression indexes on these text fields. CREATE INDEX mytable_start_time_idx ON mytable (start_time::timestamp); CREATE INDEX mytable_end_time_idx ON mytable (end_time::timestamp);
All you say is true. Index is the right way to go, and yes, I copied the query from the question itself keeping the mistake in it :)
One more bug in your answer. Timestamp < Date comparisons. One must be aware that '2016-12-31 23:55:00' is NOT lesser than '2016-12-31'. So your query will NOT include anything later than '2016-12-31 00:00:00'. Right side should be end_time::timestamp <= '2016-12-31 23:59:59.999' Or even better - end_time::date <= '2016-12-31' (which is simpler to type).
You are right again. It was just an example, though.

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.