How do you insert a properly formatted datetime literal into SQL Server? | A1Lab@HumairaM to SQLEnglish • 2 days ago

How do you insert a properly formatted datetime literal into SQL Server?

message-square
0

To insert a properly formatted date or datetime literal into SQL Server (T-SQL) without regional/date-format ambiguity (MDY vs. DMY), use unambiguous ISO/ODBC string literals—‘YYYY-MM-DD’ for DATE and ‘YYYY-MM-DDTHH:mm:ss[.fractional]’ (ISO 8601 without spaces) for DATETIME and DATETIME2—or pass a typed parameter/CAST explicitly to DATETIME2.

  1. Unambiguous Date and Datetime Literals Avoid ambiguous slash or locale-dependent string formats like ‘11/30/2026’ or ‘30/11/2026’, which can fail or swap month and day depending on SET LANGUAGE or SET DATEFORMAT.

‘YYYY-MM-DD’: Unambiguous for DATE, DATETIME, DATETIME2, SMALLDATETIME, and DATETIMEOFFSET.

‘YYYY-MM-DDTHH:mm:ss’ (with T, no spaces): Unambiguous across all DATETIME, DATETIME2, and DATETIMEOFFSET types regardless of session DATEFORMAT.

‘YYYY-MM-DD HH:mm:ss’ (with a space): Safe for DATE, DATETIME2, and DATETIMEOFFSET, but can be affected by SET DATEFORMAT on legacy DATETIME / SMALLDATETIME in older SQL Server compatibility modes—prefer ‘T’ or DATETIME2.

SQL INSERT INTO dbo.orders (customer_id, order_date, created_at, scheduled_at) VALUES (42, ‘2026-11-30’, ‘2026-11-30T19:05:00’, ‘2026-11-30T19:05:00.125’), (43, ‘2026-11-30’, CAST(‘2026-11-30 19:05:00’ AS DATETIME2(0)), ‘2026-11-30T19:05:00+05:00’); 2. Type-Specific Precision and Range Notes Target Column Type Recommended Literal Format Notes & Pitfalls DATE ‘YYYY-MM-DD’ Stores date only (0001-01-01 to 9999-12-31). DATETIME2(n) ‘YYYY-MM-DDTHH:mm:ss[.ffffff]’ Preferred over legacy DATETIME; up to 7 fractional digits (100 ns), wider range (0001-01-01 to 9999-12-31). DATETIME ‘YYYY-MM-DDTHH:mm:ss[.mmm]’ Legacy type; 1753-01-01 to 9999-12-31, rounds milliseconds to .000, .003, or .007 increments. SMALLDATETIME ‘YYYY-MM-DDTHH:mm:ss’ Range 1900-01-01 to 2079-06-06; minute precision (rounds seconds to nearest minute). DATETIMEOFFSET(n) ‘YYYY-MM-DDTHH:mm:ss[.fffffff]+HH:mm’ Always include the explicit offset (such as +05:00 or Z) rather than relying on implicit conversion. 3. Using Typed Parameters and Current Timestamp Functions In application code or stored procedures, pass typed date/datetime parameters instead of string concatenation to prevent conversion errors and SQL injection. For current timestamps:

SYSDATETIME() / SYSUTCDATETIME(): Higher precision (DATETIME2) for local server time or UTC.

GETDATE() / GETUTCDATE(): Returns DATETIME for local server time or UTC.

SQL DECLARE @order_date DATE = ‘2026-11-30’; DECLARE @created_at DATETIME2(3) = ‘2026-11-30T19:05:00.000’;

INSERT INTO dbo.orders (customer_id, order_date, created_at) VALUES (42, @order_date, @created_at); 4. Operational Checklist Use ‘YYYY-MM-DD’ for dates and ISO 8601 ‘YYYY-MM-DDTHH:mm:ss[.fff]’ for datetimes to avoid locale/language parsing bugs.

Default to DATETIME2 (rather than legacy DATETIME) for new columns to get cleaner fractional precision, wider date range, and predictable behavior.

Include explicit time zone offsets (+HH:MM / -HH:MM) when inserting into DATETIMEOFFSET.

Use typed parameters in application queries rather than building SQL strings dynamically.