// dates & times reference

SQL CONVERT style numbers table

An integer style code tells SQL Server's CONVERT how to render or parse a date: a lookup table, not a format string.

Категория
Dates & times
Returns
varchar / datetime
In this category
94 of 121

Синтаксис

CONVERT(varchar, date, style)
CONVERT(datetime, text, style)

Each block is a separate form. The support table and the examples show which engine takes which.

Поддержка движка

ДвижокSupportЗаметки
SQL Server Да Supported under this name.
PostgreSQL Not built in No built-in equivalent.
MySQL Not built in No built-in equivalent.
SQLite Not built in No built-in equivalent.
Oracle Not built in TO_CHAR / TO_TIMESTAMP with format masks

Примеры

SQL Server

SELECT CONVERT(varchar(19), CAST('2026-08-01 14:30' AS datetime), 120);  -- 2026-08-01 14:30:00

SQL Server

SELECT CONVERT(varchar(8), CAST('2026-08-01' AS date), 112);  -- 20260801

SQL Server

SELECT CONVERT(DATETIME2, '2026-08-01 14:30:00', 120);

Полезно знать

  • The styles that matter: 112 gives yyyymmdd (sorts and joins reliably), 120 gives yyyy-mm-dd hh:mi:ss (ISO-ish, ODBC canonical), 126 gives full ISO 8601 with T, 101 gives US mm/dd/yyyy.
  • Styles above 100 add century digits to the two-digit-year variants: 1 is mm/dd/yy while 101 is mm/dd/yyyy. The two-digit forms are parse hazards; skip them.
  • When parsing, the style must match the input, not the desired output: CONVERT(datetime, '01/08/2026', 103) reads British day-first order.

Questions about CONVERT styles

Which style is the safe default?

120 for round-trippable text with time, 112 for compact keys, 126 for ISO 8601 interchange. Avoid 1-99's two-digit years entirely.

What do other engines use instead of style codes?

Format masks: PostgreSQL and Oracle TO_CHAR with 'YYYY-MM-DD', MySQL DATE_FORMAT with '%Y-%m-%d', SQLite strftime. FORMAT() in SQL Server is the .NET-mask compromise.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.