DATETIME2 and SMALLDATETIME
SQL Server ships three datetimes with no zone. DATETIME2 is the modern one, DATETIME is the legacy type that starts in 1753 and rounds to 3.33 ms, and SMALLDATETIME rounds to whole minutes.
- Family
- Dates & times
- Missing in
- None of the five
- Gotchas
- 5 listed
Engine by engine
SQL Server's DATETIME2 (6-8 bytes, 100 ns, years 1-9999), legacy DATETIME (8 bytes, 3.33 ms) and SMALLDATETIME (4 bytes, minutes, 1900-2079), with each other engine's nearest equivalent.
| 引擎 | 類型 | 儲存 | 備註 |
|---|---|---|---|
| SQL Server |
DATETIME2 / SMALLDATETIME
|
6 to 8 / 4 bytes | DATETIME2(p): 6 bytes at p 0-2 up to 8 at p 7. SMALLDATETIME is 4 bytes with minute precision, range 1900-01-01 to 2079-06-06. |
| PostgreSQL |
TIMESTAMP(p)
|
8 bytes | timestamp(0) is the SMALLDATETIME stand-in: whole seconds, still 8 bytes. PostgreSQL has no minute-rounded type. |
| MySQL |
DATETIME
|
5 to 8 bytes | DATETIME(0) gives whole seconds in 5 bytes; range 1000-9999 dwarfs SMALLDATETIME's 1900-2079. |
| SQLite |
TEXT
|
variable | No equivalent. ISO-8601 text; truncate to minutes or seconds yourself before storing. |
| Oracle |
TIMESTAMP
|
7 to 11 bytes | Oracle DATE (7 bytes) stores whole seconds, the closest match to SMALLDATETIME; TIMESTAMP(p) adds up to 9 fractional digits. |
Gotchas
Where the engines disagree. These tend to surface as bugs during a migration or a port.
- Legacy DATETIME rounds to .000, .003 or .007 (a 1/300-second grid), so a value like 12:00:00.005 silently becomes .007 and equality lookups miss. DATETIME2 does not round this way.
- DATETIME starts at 1753-01-01, the first full year after Britain and its colonies switched to the Gregorian calendar; DATETIME2 covers 0001-01-01 to 9999-12-31. Historical data needs DATETIME2.
- SMALLDATETIME rounds to the nearest minute (29.998 seconds or less round down) and stops at 2079-06-06, which 30-year mortgage and lease end dates already pass.
- MySQL's DATETIME and SQL Server's DATETIME share a name but nothing else: MySQL's is zone-free wall-clock time with a 5-byte base; SQL Server's is the 8-byte 3.33 ms legacy type.
- DATETIME2 costs the same 8 bytes as legacy DATETIME at full precision (less below p 5), so there is no storage reason left to pick DATETIME for new tables.
Common questions
Should I use DATETIME2 or DATETIME in SQL Server?
DATETIME2. It has a wider range (year 1 to 9999), 100-nanosecond precision, and the same or smaller storage. Keep DATETIME only for compatibility with old code that depends on the 3.33 ms rounding.
What is SMALLDATETIME good for?
Minute-granularity schedules where 4 bytes matter. It rounds to the nearest minute and cannot store dates past 2079-06-06, which is closer than it sounds.
How many bytes is DATETIME2?
6 bytes at precision 0-2, 7 bytes at 3-4, and 8 bytes at 5-7. Legacy DATETIME is always 8 bytes, and SMALLDATETIME is 4.
Map a type to the other four engines
The free type converter maps a type from one engine to its closest equivalent in the others and says where storage, precision or behaviour differ. The desktop app does the same mapping while you edit.