

This tutorial discusses ways to calculate the difference in days between dates in PostgreSQL. However, the date difference can be obtained using expressions containing other functions provided by PostgreSQL. Write queries for continuous periods as explicit range condition. Unlike SQL Server, PostgreSQL does not have a built-in DATEDIFF function to calculate the difference between dates. Of course you could calculate the boundary dates in your application if you wish. , DATEADD(mm, 3, can use similar auxiliary functions for other periods-most of them will be less complex than the examples above, especially when using greater than or equal to ( >=) and less than ( <) conditions instead of the between operator. $$ LANGUAGE plpgsql SQL Server CREATE FUNCTION quarter_begin DATETIME )ĮND CREATE FUNCTION quarter_end DATETIME ) $$ LANGUAGE plpgsql CREATE FUNCTION quarter_end(dt timestamp with time zone) the Oracle DATE type has seconds resolutionĮND PostgreSQL CREATE FUNCTION quarter_begin(dt timestamp with time zone) , INTERVAL -1 MICROSECOND) Oracle CREATE FUNCTION quarter_begin(dt IN DATE)ĮND CREATE FUNCTION quarter_end(dt IN DATE) ( DATE_ADD ( quarter_begin(dt), INTERVAL 3 MONTH ) ) CREATE FUNCTION quarter_end(dt DATETIME) RETURN TRUNC(dt, 'Q') + 3 MONTHS - 1 SECOND MySQL CREATE FUNCTION quarter_begin(dt DATETIME) RETURN TRUNC(dt, 'Q') CREATE FUNCTION quarter_end(dt TIMESTAMP) The following examples show implementations of the functions QUARTER_BEGIN and QUARTER_END for various databases.ĭB2 CREATE FUNCTION quarter_begin(dt TIMESTAMP) This logic can be hidden in the function. The QUARTER_END function must therefore return a time stamp just before the first day of the next quarter if the SALE_DATE has a time component. The calculation can become a little complex because the between operator always includes the boundary values. The functions QUARTER_BEGIN and QUARTER_END compute the boundary dates. If you have done your homework, you probably recognize the pattern from the exercise about all employees who are 42 years old.Ī straight index on SALE_DATE is enough to optimize this query. This is a generic solution that works for all databases: SELECT. The alternative is to use an explicit range condition. However the solution from above does not apply to MySQL prior to version 5.7, because MySQL didn’t support function-based indexing before that version. The query uses a date format that only contains year and month: again, this is an absolutely correct query that has the same problem as before. The problem also occurs with databases that have a pure date type if you search for a longer period as shown in the following MySQL query: SELECT.

If you use it inconsistently-sometimes with, sometimes without TRUNC-then you need two indexes! CREATE INDEX index_nameīut then you must always use TRUNC(sale_date) in the where clause. There is a rather simple solution for this problem: a function-based index.
#Postgresql datediff code#
Use the discount code “Summer22” and save 10€ on my book.

I make my living from SQL training, SQL tuning and consulting and my book “SQL Performance Explained”.
