DATEDIFF Function - SQL Server to PostgreSQL Migration

In SQL Server, you can use DATEDIFF function to get the datetime difference in specified units. In PostgreSQL, you have to use various INTERVAL expressions.

SQL Server:

  -- Get difference in days
  SELECT DATEDIFF(dd, '2022-09-01', '2022-09-06'); 
  # 5

PostgreSQL:

  -- Get difference in days (note that the order of datetime values is different now!)
  SELECT EXTRACT(DAY FROM '2022-09-06'::TIMESTAMP - '2022-09-01'::TIMESTAMP); 
  # 5

For more information, see SQL Server to PostgreSQL Migration