Are you using SqlServer?

Take a look at the style parameter of CONVERT() here: http://msdn.microsoft.com/en-us/library/ms187928.aspx - in particular have a look at the ODBC canonical (with milliseconds) format.

For short you should use style 121 in the CONVERT() command.

Style: 121

ODBC canonical (with milliseconds) default for time, date, datetime2, and datetimeoffset

yyyy-mm-dd hh:mi:ss.mmm(24h)

Answer from Widor on Stack Overflow
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ sql โ€บ t-sql โ€บ functions โ€บ cast-and-convert-transact-sql
CAST and CONVERT (Transact-SQL) - SQL Server | Microsoft Learn
For conversion from datetime or smalldatetime to character data, see the previous table for the output format. 5 Hijri is a calendar system with several variations. SQL Server uses the Kuwaiti algorithm. 6 For a milliseconds (mmm) value of 0, the millisecond decimal fraction value won't display.
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ sql-display-milliseconds-to-date-format
sql display milliseconds to date format โ€“ SQLServerCentral Forums
November 11, 2018 - ... ... You will have to divide the values in hours and milliseconds. ๐Ÿ˜Ž ยท DECLARE @BMS BIGINT = 1542004320409; DECLARE @BH INT = @BMS / 3600000; DECLARE @IMS INT = @BMS % 3600000; SELECT DATEADD(MILLISECOND,@IMS,DATEADD(HOUR,@BH,CONVERT(DATETIME2(3),'19700101',112)));
๐ŸŒ
Databasefaqs
databasefaqs.com โ€บ home โ€บ sql server convert datetime to string + examples
SQL Server Convert Datetime to String + Examples - DatabaseFAQs.com
March 23, 2023 - In SQL Server, we can use the Convert() function to convert Datetime to a string with milliseconds.
๐ŸŒ
SQLines
sqlines.com โ€บ sql-server-to-postgresql โ€บ convert_string_datetime
CONVERT - Datetime to String - SQL Server to PostgreSQL Migration - SQLines Tools
In SQL Server, you can use the CONVERT function to convert a DATETIME value to a string with the specified format. In PostgreSQL, you can use the TO_CHAR function. Note that SQL Server CONVERT and PostgreSQL TO_CHAR formats are different. ... -- 3rd parameter specifies 121 style (ODBC 'YYYY-MM-DD ...
๐ŸŒ
DotFactory
dofactory.com โ€บ sql โ€บ convert-datetime-to-string
SQL Convert DATETIME to String
The VARCHAR argument specifies that the output is a string value. ... The CONVERT function can convert datetime to string values.
Find elsewhere
๐ŸŒ
SQL Shack
sqlshack.com โ€บ sql-convert-date-functions-and-formats
SQL Convert Date functions and formats
May 21, 2021 - Let us explore various date formats using SQL convert date functions. First, we declare a variable to hold current DateTime using the SQL GETDATE() function with the following query.
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ func_sqlserver_convert.asp
SQL Server CONVERT() Function
String Functions: Asc Chr Concat with & CurDir Format InStr InstrRev LCase Left Len LTrim Mid Replace Right RTrim Space Split Str StrComp StrConv StrReverse Trim UCase Numeric Functions: Abs Atn Avg Cos Count Exp Fix Format Int Max Min Randomize Rnd Round Sgn Sqr Sum Val Date Functions: Date DateAdd DateDiff DatePart DateSerial DateValue Day Format Hour Minute Month MonthName Now Second Time TimeSerial TimeValue Weekday WeekdayName Year Other Functions: CurrentUser Environ IsDate IsNull IsNumeric SQL Quick Ref
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sql โ€บ sql-query-to-convert-datetime-to-string
SQL Query to Convert Datetime to String - GeeksforGeeks
July 23, 2025 - /*Declaring DATETIME as dt*/ DECLARE @dt DATETIME = (SELECT order_date FROM orders WHERE prod_id = 101); /*SELECT statement is used to print the s1 message*/ SELECT CONVERT(VARCHAR(20),@dt,0) s1;
๐ŸŒ
Dylanbeattie
dylanbeattie.net โ€บ 2015 โ€บ 07 โ€บ 29 โ€บ the-mysterious-case-of-missing.html
The Mysterious Case of the Missing Milliseconds : dylanbeattie.net
SELECT @DateTime = '2015-07-29 21:59:15:001' SELECT CONVERT(VARCHAR(128), @DateTime, 126) -- returns 2015-07-29T21:59:15 ยท First, SQL Server doesn't have true millisecond precision - the milliseconds part will often get rounded by +- 0.001 seconds.