Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL- Filter a date range

I'm a SQL developer and spend most of my time in MSSQL. I'm looking for a better way to filter a "Timestamp without timezone" field in a PostgreSQL DB.

I'm using:

Where 
DateField >= '2010-01-01' and 
DateField < '2012-01-01'

But given that I'm not an expert at the syntax I have to think there's a better way.

Any Suggestions? Thanks.

like image 957
Rob S Avatar asked Apr 04 '13 17:04

Rob S


People also ask

How do I search between dates in PostgreSQL?

Use between to Query Between Date Ranges in PostgreSQL You can run the SQL command below to see who logged in between 2021 and the current date. Here the format of the date data type is YYYY-MM-DD. So, when you try to insert or write the query, make sure that you use the format supported by the PostgreSQL database.

What is date range in PostgreSQL?

In Postgresql, Range types are data types that represent a range of values of some element type. There are many different range types in Postgresql and daterange is one of the types that represent the range of date. Let' view the records of employees whose hire date range between 1985-11-21 and 1989-06-02.

Is between inclusive in PostgreSQL?

The PostgreSQL BETWEEN condition will return the records where expression is within the range of value1 and value2 (inclusive).

How do I get the current time in PostgreSQL?

Postgresql now() The NOW() function in Postgresql is used to get the current date and time. The return type of the NOW() function is the timestamp with the time zone. We can fetch the current date and time by using the PostgreSQL NOW() function. This function has a return type i.e. the timestamp with the time zone.


5 Answers

Your solution is fine. If the dates are literals, I'd prefer, though:

WHERE datefield >= '2010-01-01 00:00:00' 
 AND  datefield <  '2012-01-01 00:00:00'

This performs exactly the same, but is more maintenable, because it makes clear the point of each literal "date" being a timestamp, not a date. For example, suppose sometime someone changes your query to the following

    AND  datefield <=  '2012-01-01'

... expecting (and failing) to include the full day "2012-01-01" in the query. With the later syntax, the intention is more clear and this confusion is prevented.

To make it even more clear (perhaps too verbose), you can do the explicit cast:

WHERE datefield >= '2010-01-01 00:00:00'::timestamp 
 AND  datefield <  '2012-01-01 00:00:00'::timestamp

I wouldn't use to_date() here for similar reasons (potential datatype confusion), nor to_timestamp() (it returns a timestamptz).

BTW, I've modified the case to comply with recommended practice (keywords in uppercase, identifiers in lowercase)

like image 139
leonbloy Avatar answered Oct 19 '22 18:10

leonbloy


For date intervals you can use something like:

WHERE DateField BETWEEN to_date('2010-01-01','YYYY-MM-DD') 
                    AND to_date('2010-01-02','YYYY-MM-DD')

It is shorter (you do not need to repeat DateField), and has explicit date format.

For 1 hour/day/month/year you can use:

WHERE date_trunc('day',DateField) = to_date('2010-01-01','YYYY-MM-DD')
like image 42
Ihor Romanchenko Avatar answered Oct 19 '22 17:10

Ihor Romanchenko


You can keep the query simple by using BETWEEN as long as your column name is of type TIMESTAMP and your column name isn't "timestamp"...

SELECT * FROM table WHERE column BETWEEN '2018-12-30 02:19:34' AND '2018-12-30 02:25:34'

This works for dates '2018-12-30' and date-times '2018-12-30 02:19:34'.

like image 3
Phil Andrews Avatar answered Oct 19 '22 18:10

Phil Andrews


I would agree with leonbloy that using this would make the code more readable and clear.

WHERE datefield >= '2010-01-01 00:00:00'::timestamp 
AND  datefield <  '2012-01-01 00:00:00'::timestamp
like image 2
Anish Jain Avatar answered Oct 19 '22 16:10

Anish Jain


You can also use interval if you want to filter for months or days etc. like this. datefield < current_date + interval '1 months'

like image 2
shyam yadav Avatar answered Oct 19 '22 18:10

shyam yadav