One concept, four spellings
Date and time handling is where SQL portability breaks down fastest. Every major engine has its own function names, argument order and timezone defaults for the same logical operations. This reference puts the common tasks — current time, adding intervals, extracting fields, truncating and formatting — side by side across MySQL, PostgreSQL, SQLite and SQL Server so you can translate at a glance.
How it works
Each task maps to one expression per dialect. The differences fall into a few predictable buckets:
-- Add seven days
DATE_ADD(d, INTERVAL 7 DAY) -- MySQL
d + INTERVAL '7 days' -- PostgreSQL
date(d, '+7 days') -- SQLite
DATEADD(DAY, 7, d) -- SQL Server
Watch three things: argument order (SQL Server puts the unit first), the
interval syntax (a keyword in MySQL, a string in Postgres), and whether the
result is a date, datetime or text value. Functions like strftime in SQLite
always return text, so wrap them in CAST(... AS INTEGER) when you need a number.
Conventions that bite
Day-of-week numbering is the classic trap: Sunday is 1 in MySQL, 0 in
PostgreSQL and SQLite, and configurable in SQL Server via SET DATEFIRST.
Unix-epoch extraction also overflows a 32-bit INT past the year 2038 in SQL
Server’s DATEDIFF(SECOND, ...), so use DATEDIFF_BIG there. When in doubt,
confirm timezone defaults — NOW() is local in some engines and UTC in others.