You need to tell SQL Server it's a date; otherwise, it just sees a string, and ignores the style number since it's not relevant for a string. As Steve Kass pointed out, the code is only truly portable if you protect the incoming string from incorrect regional- or language-based translations (such as d/m/y - which could lead to an error or, even worse, the wrong data). I've updated the code to interpret the string as m/d/y regardless of locale, but if you're on SQL Server 2012 you could also use PARSE() as in his example (or TRY_PARSE() if you want to essentially ignore invalid dates).

And if you want the time attached including milliseconds, you need to allow more than 10 characters, and a style that supports milliseconds.

SELECT CONVERT(CHAR(23),CONVERT(DATETIME,'7/19/2013',101),121);

Result:

2013-07-19 00:00:00.000

If you don't care about milliseconds, you can use style 120 instead:

SELECT CONVERT(CHAR(19),CONVERT(DATETIME,'7/19/2013',101),120);

And if you don't care about seconds, you can truncate earlier:

SELECT CONVERT(CHAR(16),CONVERT(DATETIME,'7/19/2013',101),120);
Answer from Aaron Bertrand on Stack Overflow
Top answer
1 of 4
16

You need to tell SQL Server it's a date; otherwise, it just sees a string, and ignores the style number since it's not relevant for a string. As Steve Kass pointed out, the code is only truly portable if you protect the incoming string from incorrect regional- or language-based translations (such as d/m/y - which could lead to an error or, even worse, the wrong data). I've updated the code to interpret the string as m/d/y regardless of locale, but if you're on SQL Server 2012 you could also use PARSE() as in his example (or TRY_PARSE() if you want to essentially ignore invalid dates).

And if you want the time attached including milliseconds, you need to allow more than 10 characters, and a style that supports milliseconds.

SELECT CONVERT(CHAR(23),CONVERT(DATETIME,'7/19/2013',101),121);

Result:

2013-07-19 00:00:00.000

If you don't care about milliseconds, you can use style 120 instead:

SELECT CONVERT(CHAR(19),CONVERT(DATETIME,'7/19/2013',101),120);

And if you don't care about seconds, you can truncate earlier:

SELECT CONVERT(CHAR(16),CONVERT(DATETIME,'7/19/2013',101),120);
2 of 4
4

Note that Aaron's solution will fail if the server is localized to a language with DMY as the date format. This is because the inner CONVERT in Aaron's example will incorporate the server locale, which may not be what you expect.

To make this bulletproof (assuming the source of the string doesn't automatically re-localize the format), convert the string with PARSE (requires SQL Server 2012 or later).

SET LANGUAGE English
SELECT CONVERT(CHAR(23),TRY_CONVERT(DATETIME,'7/19/2013'),121);
SELECT CONVERT(CHAR(23),PARSE('7/19/2013' AS DATETIME USING 'en-US'),121);

SET LANGUAGE Français
SELECT CONVERT(CHAR(23),TRY_CONVERT(DATETIME,'7/19/2013'),121);
SELECT CONVERT(CHAR(23),PARSE('7/19/2013' AS DATETIME USING 'en-US'),121);
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › adding-hhmmss-time-to-yyyy-mm-dd-hhmmss-format
adding hh:mm:ss time to yyyy-mm-dd hh:mm:ss format – SQLServerCentral Forums
December 10, 2021 - INSERT INTO [portman].[dbo].[AgentHours] ([DOMAIN] ,[LOGIN TIMESTAMP] ,[LOGIN TIME] ,[LOGOUT TIMESTAMP] ) SELECT 'EVQ' --literal ,cast([created_at] as datetime) --yyyy-mm-dd hh:mm:ss format ,cast([event_sec] as time) --hh:mm:ss format ,DATEADD([created_at] + [event_sec]) --yyyy-mm-dd hh:mm:ss format FROM [Portman].[dbo].[ConvoHrs]
Discussions

SQL accepts this format for datetime yyyy-MM-dd HH:mm:ss and I am getting MM/dd/yyyy HH:mm:ss format
I should pass a datetime variable but I am not able to. the time will be Datetime.Now I want this to be updated in datetime type with yyyy-MM-dd HH:mm:ss format More on forum.uipath.com
🌐 forum.uipath.com
1
0
September 25, 2024
sql server - Date format in dd/MM/yyyy hh:mm:ss - Stack Overflow
I need to convert datetime from 2012-07-29 10:53:33.010 to 29/07/2012 10:53:33. I tried using select CONVERT(varchar(20), GETDATE(), 131) but its showing date according to Hijri calendar 11/09/1... More on stackoverflow.com
🌐 stackoverflow.com
Date Format (mm/dd/yyyy hh:mm), no seconds or milliseconds – SQLServerCentral Forums
Date Format (mm/dd/yyyy hh:mm), no seconds or milliseconds Forum – Learn more on SQLServerCentral More on sqlservercentral.com
🌐 sqlservercentral.com
February 9, 2009
Converting yyyy/mm/dd hh:mm:ss – SQLServerCentral Forums
There is only one display format in ANSI SQL; the ISO-8601 version with dashes "yyyy-mm-dd HH:MM:SS.sssssss". More on sqlservercentral.com
🌐 sqlservercentral.com
July 11, 2012
🌐
Microsoft Learn
learn.microsoft.com › en-us › sql › t-sql › data-types › date-transact-sql
date (Transact-SQL) - SQL Server | Microsoft Learn
The default string literal format, which is used for down-level clients, complies with the SQL standard form that is defined as yyyy-MM-dd.
🌐
MSSQLTips
mssqltips.com › home › sql date format examples using convert function
SQL Date Format Examples using SQL CONVERT Function
September 26, 2025 - The date used for all of these examples is “2022-12-30 00:38:54.840” (December 30, 2022). The format is yyyy-mm-dd hh:mm:ss:nnn.
🌐
UiPath Community
forum.uipath.com › help › solution accelerators
SQL accepts this format for datetime yyyy-MM-dd HH:mm:ss and I am getting MM/dd/yyyy HH:mm:ss format - Solution Accelerators - UiPath Community Forum
September 25, 2024 - I should pass a datetime variable but I am not able to. the time will be Datetime.Now I want this to be updated in datetime type with yyyy-MM-dd HH:mm:ss format
🌐
SQL Shack
sqlshack.com › sql-convert-date-functions-and-formats
SQL Convert Date functions and formats
May 21, 2021 - Suppose you have data in a table in the format YYYY-MM-DD hh:mm: ss. You have a daily Sales report, and in that, you want data group by date.
🌐
MSSQLTips
mssqltips.com › home › format sql server dates with format function
Format SQL Server Dates with FORMAT Function
October 31, 2025 - Can you update your second date sample? SELECT FORMAT (getdate(), ‘dd/MM/yyyy, hh:mm:ss ‘) as date for 2 PM would return 02:00:00.
Find elsewhere
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › converting-yyyymmdd-hhmmss
Converting yyyy/mm/dd hh:mm:ss – SQLServerCentral Forums
July 11, 2012 - There is only one display format in ANSI SQL; the ISO-8601 version with dashes "yyyy-mm-dd HH:MM:SS.sssssss".
🌐
W3Schools
w3schools.com › sql › sql_dates.asp
Date Functions in SQL Server and MySQL
DATETIME - format: YYYY-MM-DD HH:MI:SS · TIMESTAMP - format: YYYY-MM-DD HH:MI:SS · TIME - format: HH:MI:SS · YEAR - format YYYY or YY · SQL Server has the following date data types: DATE - format YYYY-MM-DD · DATETIME - format: YYYY-MM-DD ...
🌐
Tutlane
tutlane.com › article › sql-server › convert-format-datetime-in-sql-server-with-examples
Convert (Format) DateTime in SQL Server with Examples - Tutlane
SELECT FORMAT(GETDATE(), 'MMM dd yyyy') --------------------------------------- Mar 22 2023 ... We can convert the datetime value to hh:mm:ss AM/PM using the FORMAT function as shown below.
🌐
MariaDB
mariadb.com › kb › en › datetime-format-yyyymmdd-hhmmssfff
Datetime format YYYYMMDD hh:mm:ss.fff - MariaDB
Comprehensive documentation, release notes and learning resources for all MariaDB products.
🌐
Quest Blog
blog.quest.com › home › various ways to use the sql convert date function
Various ways to use the SQL CONVERT date function - Blogs
April 26, 2024 - HH: It gives 24 hrs. format hour 00 to 23 ... In the below script, we converted the input date formats into multiple formats using the above abbreviations or codes. DECLARE @d DATETIME ='2020-12-08 16:36:17.760'; SELECT FORMAT (@d,'dd/MM/yyyy ') as [Date Format 1] , FORMAT (@d, 'dd/MM/yyyy, hh:mm:ss ') as [Date Format 2] , FORMAT(@d,'yyyy-MM-dd HH:mm:ss')as [Date Format 3] , FORMAT(@d,'Dd MMM yyyy HH:mm:ss')as [Date Format 4] , FORMAT(@d,'MMM d yyyy h:mm:ss')as [Date Format 5] , FORMAT (@d, 'dddd, MMMM, yyyy')as [Date Format 6] , FORMAT (@d, 'MMM dd yyyy') as [Date Format 7] , FORMAT (@d, 'MM.dd.yy') as [Date Format 8] , FORMAT (@d, 'MM-dd-yy') as [Date Format 9] , FORMAT (@d, 'hh:mm:ss tt')as [Date Format 10] , FORMAT (@d, 'd-M-yy')as [Date Format 11] , FORMAT(@d,'MMMM dd,yyyy')as [Date Format 12]
🌐
TutorialsPoint
tutorialspoint.com › setup-the-format-of-datetime-to-ddmm-yyyy-hh-mm-ss-with-mysql-select
Setup the format of DATETIME to 'DDMM- YYYY HH:MM:SS' with MySQL SELECT?
Whenever you retrieve datetime from a table, the datetime gives ‘YYYY-MM-DD’ format. If you want to change the output, then you need to use in-built date_format() from MySQL. The syntax is as follows −