SQL Server's dialect, T-SQL, differs from PostgreSQL in many small ways. The most common translations:
| Task |
SQL Server |
Standard / PostgreSQL (and here) |
| First n rows |
SELECT TOP 5 ... |
... LIMIT 5 |
| NULL fallback |
ISNULL(a, b) |
COALESCE(a, b) (works in SQL Server too) |
| Inline if |
IIF(cond, a, b) |
CASE WHEN cond THEN a ELSE b END |
| Add days |
DATEADD(day, 7, d) |
d + INTERVAL 7 DAY |
| Days between |
DATEDIFF(day, a, b) |
b - a or DATE_DIFF('day', a, b) |
| Join text |
a + ' ' + b |
`a |
| Quote a name |
[order] |
"order" |
| Length |
LEN(s) |
LENGTH(s) |
COALESCE, CASE and CONCAT work in every major database, so writing them by default makes code portable. One behavior trap: LEN ignores trailing spaces, LENGTH counts them.