Format SQL Server date/time output with CONVERT()

Use this example to see the core idea, then work through the complete steps below. Code snippets are Classic ASP / VBScript or SQL Server T-SQL as labelled, not PHP.

Format a SQL Server datetime for display

SQL example

SELECT CONVERT(char(10), issue_dt, 23) AS iso_date,
       CONVERT(char(8), issue_dt, 108) AS time_24h
FROM dbo.dt;

Style 23 produces yyyy-mm-dd; style 108 produces hh:mm:ss. For storing or comparing values, retain the underlying date/time data type.

On this page — Show page sectionsHide page sections

Use SQL Server CONVERT() when a query specifically needs a textual date/time representation. Keep the underlying column typed as date, datetime2 or another date/time type; formatting is a presentation step.

Time only

rs.Open "SELECT CONVERT(char(8), issue_dt, 108) AS formatted_time FROM dbo.dt", conn

Style 108 produces a 24-hour hh:mm:ss representation.

22:02:00

Using a shorter char length truncates the displayed text, but an explicit target length matching the intended format is clearer.

Date only in U.S. display format

rs.Open "SELECT CONVERT(char(10), issue_dt, 101) AS formatted_date FROM dbo.dt", conn
12/22/2007

Style 101 uses mm/dd/yyyy. For machine-readable interchange, prefer an ISO style such as 23 (yyyy-mm-dd) or 126.

Common CONVERT() date/time styles

StyleNameTypical output
0 or 100Default datetimemon dd yyyy hh:miAM
101U.S.mm/dd/yyyy
102ANSIyyyy.mm.dd
103British/Frenchdd/mm/yyyy
104Germandd.mm.yyyy
105Italiandd-mm-yyyy
106Day mon yeardd mon yyyy
107Mon day, yearMon dd, yyyy
108Timehh:mi:ss
109Default + millisecondsmon dd yyyy hh:mi:ss:mmmAM
110USAmm-dd-yyyy
111Japanyyyy/mm/dd
112ISO compactyyyymmdd
113Europe + millisecondsdd mon yyyy hh:mi:ss:mmm
114Time + millisecondshh:mi:ss:mmm
120ODBC canonicalyyyy-mm-dd hh:mi:ss
121ODBC canonical + millisecondsyyyy-mm-dd hh:mi:ss.mmm
23ISO dateyyyy-mm-dd
126ISO 8601yyyy-mm-ddThh:mi:ss.mmm
127ISO 8601 with Zyyyy-MM-ddThh:mm:ss.fffZ
130Hijridd mon yyyy hh:mi:ss:mmmAM
131Hijridd/mm/yyyy hh:mi:ss:mmmAM
Styles that use two-digit years can be ambiguous and some style conversions are nondeterministic. Prefer four-digit-year and ISO representations when the value will be parsed again rather than merely displayed.

Formatting versus data storage

CONVERT() with a style number can produce human-readable strings, but sorting and filtering should generally use real date/time values. Avoid converting indexed date columns in WHERE conditions.

Unambiguous output

Use style 23 for ISO-like yyyy-mm-dd and style 126 for ISO 8601 date-time text. Display formats are a presentation choice, not a substitute for parameterized data storage.

Frequently asked questions

Are these queries for SQL Server or Access?
The SQL pages use SQL Server T-SQL. Access has different date functions, identity behavior, and SQL syntax.
Can I execute these statements directly on a live database?
Start in a test database. INSERT, UPDATE, DELETE, DROP and CREATE change data or schema.
How should Classic ASP send input to a query?
Use an ADODB.Command with typed, ordered parameters and encode database values when writing HTML.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer