Date Difference Function

posted Apr 28, 2017, 11:51 AM by Sachchida Ojha   [ updated Apr 28, 2017, 11:52 AM ]
PostgreSQL does not provide DATEDIFF function similar to SQL Server DATEDIFF, but you can use various expressions or UDF to get the same results.
SQL Server and SybasePostgreSQL
YearsDATEDIFF(yy, start, end)DATE_PART('year', end) - DATE_PART('year', start)
MonthsDATEDIFF(mm, start, end)years_diff * 12 + (DATE_PART('month', end) - DATE_PART('month', start))
DaysDATEDIFF(dd, start, end)DATE_PART('day', end - start)
WeeksDATEDIFF(wk, start, end)TRUNC(DATE_PART('day', end - start)/7)
HoursDATEDIFF(hh, start, end)days_diff * 24 + DATE_PART('hour', end - start )
MinutesDATEDIFF(mi, start, end)hours_diff * 60 + DATE_PART('minute', end - start )
SecondsDATEDIFF(ss, start, end)minutes_diff * 60 + DATE_PART('minute', end - start )

Comments