Home > Documentation > PostgreSQL to MS SQL

Convert PostgreSQL views into SQL Server format

[PostgreSQL to SQL Server Converter]  [Migrate Triggers]  [Types Mapping

When migrate a database from SQL Server to PostgreSQL one of the steps is conversion of views into the destination format. This whitepaper discovers basic rules of this conversion.

  1. SELECT ... OFFSET X LIMIT Y is converted into
    SELECT ... OFFSET X ROWS FETCH NEXT Y ROWS ONLY;
    

  2. PostgreSQL built-in function POSITION must be reiplaced by MS SQL equivalent CHARINDEX
  3. PostgreSQL expression $date + $interval must be converted into SQL Server function call DATEADD($interval, $n_units, $date) according to the table below:

    PostgreSQL Interval SQL Server Equivalent
    ($date + $n_units * interval '1 day')::date DAY/DD/D
    ($date + $n_units * interval '1 hour')::date HOUR/HH
    ($date + $n_units * interval '1 minute')::date MINUTE/MI/N
    ($date + $n_units * interval '1 month')::date MONTH/MM/M
    ($date + $n_units * interval '1 second')::date SECOND/SS/S
    ($date + $n_units * interval '1 week')::date WEEK/WW/WK
    ($date + $n_units * interval '1 year')::date YEAR/YY

  4. Every occurrence of DATE_PART must be replaced by DATEPART
  5. PostgreSQL function NOW() must be converted into SQL Server GETDATE
  6. Every occurrence of COALESCE must be replaced by ISNULL
  7. PostgreSQL function REPEAT must be converted into SQL Server equivalent REPLICATE