CONVERT(datetime, '24.04.2012', 104)

Should do the trick. See here for more info: CAST and CONVERT (Transact-SQL)

Answer from Chris Roberts on Stack Overflow
🌐
SQL Shack
sqlshack.com › sql-server-functions-for-converting-string-to-date
SQL Server functions for converting a String to a Date
May 21, 2021 - Explicit where conversions are ... or explicitly in SQL Server using built-in functions such as CAST(), TRY_CAST(), CONVERT(), TRY_CONVERT() and TRY_PARSE()....
Discussions

Convert string to date
Hi all this has really baffled me for a few days and am seeking help. I am trying to handle strings from a free format field and either convert them to a date in the format of "dd/mm/yyyy" or if they are not in this format then simply display the text verbatim. More on forums.sqlteam.com
🌐 forums.sqlteam.com
0
August 12, 2021
convert string to date
hi I have a date in below format '2020-08-17T16:28:46' as string want to convert in date please help i am trying as below convert(DATETIME,format(insta_map_date,'dd-MMM-yyyy'),103)) the error is Argument data type varchar is invalid for… More on learn.microsoft.com
🌐 learn.microsoft.com
3
0
January 6, 2022
Convert Text to Date DataType – SQLServerCentral Forums
Convert Text to Date DataType Forum – Learn more on SQLServerCentral More on sqlservercentral.com
🌐 sqlservercentral.com
August 14, 2012
[MSSQL] Struggling with converting string to date type
converting string to date and vice versa is fun in MSSQL (the kind of fun one can only expect by having one's toenails pulled out while under salty lemon juice). So, the first thing you should check, is whether you are actually using the correct language! add set language english right at the start of your script. Not sure it will solve your particular problem, but it might well do it. More on reddit.com
🌐 r/SQL
7
6
October 29, 2018
Top answer
1 of 16
368

Try this

Cast('7/7/2011' as datetime)

and

Convert(DATETIME, '7/7/2011', 101)

See CAST and CONVERT (Transact-SQL) for more details.

2 of 16
62

Run this through your query processor. It formats dates and/or times like so and one of these should give you what you're looking for. It wont be hard to adapt:

Declare @d datetime
select @d = getdate()

select @d as OriginalDate,
convert(varchar,@d,100) as ConvertedDate,
100 as FormatValue,
'mon dd yyyy hh:miAM (or PM)' as OutputFormat
union all
select @d,convert(varchar,@d,101),101,'mm/dd/yy'
union all
select @d,convert(varchar,@d,102),102,'yy.mm.dd'
union all
select @d,convert(varchar,@d,103),103,'dd/mm/yy'
union all
select @d,convert(varchar,@d,104),104,'dd.mm.yy'
union all
select @d,convert(varchar,@d,105),105,'dd-mm-yy'
union all
select @d,convert(varchar,@d,106),106,'dd mon yy'
union all
select @d,convert(varchar,@d,107),107,'Mon dd, yy'
union all
select @d,convert(varchar,@d,108),108,'hh:mm:ss'
union all
select @d,convert(varchar,@d,109),109,'mon dd yyyy hh:mi:ss:mmmAM (or PM)'
union all
select @d,convert(varchar,@d,110),110,'mm-dd-yy'
union all
select @d,convert(varchar,@d,111),111,'yy/mm/dd'
union all
select @d,convert(varchar,@d,12),12,'yymmdd'
union all
select @d,convert(varchar,@d,112),112,'yyyymmdd'
union all
select @d,convert(varchar,@d,113),113,'dd mon yyyy hh:mm:ss:mmm(24h)'
union all
select @d,convert(varchar,@d,114),114,'hh:mi:ss:mmm(24h)'
union all
select @d,convert(varchar,@d,120),120,'yyyy-mm-dd hh:mi:ss(24h)'
union all
select @d,convert(varchar,@d,121),121,'yyyy-mm-dd hh:mi:ss.mmm(24h)'
union all
select @d,convert(varchar,@d,126),126,'yyyy-mm-dd Thh:mm:ss:mmm(no spaces)'
🌐
SQL Tutorial
sqltutorial.org › home › sql date functions › sql convert string to date functions
SQL Convert String to Date Functions: CAST() and TO_DATE()
April 4, 2020 - This tutorial shows you how to use the CAST() and TO_DATE() to convert a string to a date in SQL.
🌐
DotFactory
dofactory.com › sql › convert-string-to-datetime
SQL Convert String to DATETIME
SELECT CONVERT(DATETIME, '2022-04-28') AS Datetime Try it live ... If no time is specified, it will be set to 00:00:00.000. CONVERT and TRY_CONVERT can convert string values to datetime.
🌐
SQLTeam
forums.sqlteam.com › transact-sql
Convert string to date - Transact-SQL - SQLTeam.com Forums
August 12, 2021 - Hi all this has really baffled me for a few days and am seeking help. I am trying to handle strings from a free format field and either convert them to a date in the format of "dd/mm/yyyy" or if they are not in this fo…
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 685628 › convert-string-to-date
convert string to date - Microsoft Q&A
January 6, 2022 - Based on your question, I believe insta_map_date is a varchar field. and based on the source data sample you provided, the date format matches with ISO8601. So you can use 126 or 127 as the style while converting to datetime.
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › convert-text-to-date-datatype
Convert Text to Date DataType – SQLServerCentral Forums
August 14, 2012 - At the end, to convert string datatype into date datatype, engine uses CASTing in both cases!
Find elsewhere
🌐
Sqlrevisited
sqlrevisited.com › 2022 › 07 › how-to-convert-string-to-date-in-sql.html
SQLrevisited: How to convert String to Date in SQL Server and T-SQL? Example tutorial
January 13, 2025 - The second methodology for changing ... switching a string over completely to date explicitly can be accomplished utilizing CONVERT()....
🌐
Reddit
reddit.com › r/sql › [mssql] struggling with converting string to date type
r/SQL on Reddit: [MSSQL] Struggling with converting string to date type
October 29, 2018 -

Hi, All.

Hoping to get some pointers on this.

Here's my select statement and an example of the output showing how I've formatted the the original column to date(Style 112 Ex. 20180101, YYYYMMDD).

SELECT TOP 1
	  Column1
	, SUBSTRING(REPLACE(REPLACE(REPLACE(REPLACE(Column1,'~LIT~',''),')',''),'(',''),'-',''),1,8) Column1Formatted
FROM Table1
Column1Column1Formatted
~LIT~(2018-12-04 12:00:00.00),~LIT~(2018-12-04 12:00:00.00),,,~LIT~(C)20181204

I then want to CONVERT() this to an actual date, so I add the below into the select.

CONVERT(DATE, SUBSTRING(REPLACE(REPLACE(REPLACE(REPLACE(TaskParms1,'~LIT~',''),')',''),'(',''),'-','.'),1,10),102)

I am getting this error message...

The input character string does not follow style 112, either change the input character string or use a different style.

I'm confused on why I cannot convert a date '20181204' to DATE type, especially when I am specifying the format that matches(112)? If I choose another format, even though it does not follow the style (YYYY-MM-DD, etc..) I then get this error message... Which makes sense.

Conversion failed when converting date and/or time from character string.

Top answer
1 of 3
11

The (formerly) accepted answer iswas incorrect as it iswas a bad and misleading test. The two queries being compared do not do the same thing due to a simple typo that causes them to not be an apples-to-apples comparison. The test in the accepted answer is unfairly biased in favor of the CAST operation. The issue is that the CONVERT operation is being done with convert(date, GETDATE()+num,20) -- a value to convert that changes per row -- while the CAST operation is being done with a simple cast(GETDATE() as date) -- a value to convert that is consistent across all rows and is replaced in the execution plan as a constant. And in fact, looking at the XML execution plan even shows the actual operation performed as being CONVERT(date,getdate(),0) !!

Insofar as my testing shows (after making them equal via using cast(GETDATE()+num as date)), the times varry with them being mostly the same (which makes sense if they are both reduced to being CONVERT anyway) or the CONVERT winning:

SET STATISTICS IO, TIME ON;
    ;with t as (
               select convert(date, GETDATE(),20) as fecha , 0 as num
             union all
             select convert(date, GETDATE()+num,20) as fecha, num+1 from t where num<1000000)
    select max(fecha)
      from t
    option (maxrecursion  0);
SET STATISTICS IO, TIME OFF;

-- 4754-07-23
--Table 'Worktable'. Scan count 2, logical reads 6000008, physical reads 0, read-ahead reads 0

-- SQL Server Execution Times:
--   CPU time = 9031 ms,  elapsed time = 9377 ms.



-- VS    

SET STATISTICS IO, TIME ON;
    ;with t as (
               select cast(GETDATE() as date) as fecha , 0 as num
             union all
             select cast(GETDATE() as date) as fecha, num+1 from t where num<1000000)
    select max(fecha)
      from t
    option (maxrecursion  0);
SET STATISTICS IO, TIME OFF;

--2016-08-26
--Table 'Worktable'. Scan count 2, logical reads 6000008, physical reads 0, read-ahead reads 0

-- SQL Server Execution Times:
--   CPU time = 8969 ms,  elapsed time = 9302 ms.




SET STATISTICS IO, TIME ON;
    ;with t as (
               select cast(GETDATE() as date) as fecha , 0 as num
             union all
             select cast(GETDATE()+num as date) as fecha, num+1 from t where num<1000000)
    select max(fecha)
      from t
    option (maxrecursion  0);
SET STATISTICS IO, TIME OFF;

-- 4754-07-23
--Table 'Worktable'. Scan count 2, logical reads 6000008, physical reads 0, read-ahead reads 0

-- SQL Server Execution Times:
--   CPU time = 9438 ms,  elapsed time = 9878 ms.

The main difference between CAST and CONVERT is that CONVERT allows for the "style" to be specified. The "style" not only allows for tailoring the output when converting a non-string to a string, but also allows for specifying the input format when converting a string to a non-string:

SELECT CONVERT(DATE, '5/10/2016', 101); -- 101 = mm/dd/yyyy
-- 2016-05-10


SELECT CONVERT(DATE, '5/10/2016', 103); -- 103 = dd/mm/yyyy
-- 2016-10-05

Now compare that functionally with CAST:

SELECT CAST('13/5/2016' AS DATE);
-- Msg 241, Level 16, State 1, Line 71
-- Conversion failed when converting date and/or time from character string.


SELECT CONVERT(DATE, '13/5/2016', 101); -- 101 = mm/dd/yyyy
-- Msg 241, Level 16, State 1, Line 76
-- Conversion failed when converting date and/or time from character string.


SELECT CONVERT(DATE, '13/5/2016', 103); -- 103 = dd/mm/yyyy
-- 2016-05-13

One additional thing to mention about CAST: because it does not have the "style" parameter, the format of the date string passed in is assumed to be that of the current culture (a session property). The current culture is denoted by the @@LANGID and @@LANGUAGE system variables. This means that the CAST statement that failed in the test directly above could succeed for a different culture / language. The following tests shows this behavior and how that same date string does work with CAST when the current language is "French" (and would work with several others, based on the values in the dateformat column in sys.syslanguages):

IF (@@LANGID <> 0) -- us_english
BEGIN
    PRINT 'Changing LANGUAGE to English...';
    SET LANGUAGE ENGLISH;
    SELECT @@LANGUAGE AS [CurrentLanguage], @@LANGID AS [LangID];
END;

SELECT @@LANGUAGE, CAST('13/5/2016' AS DATE) AS [Test 1];
-- Msg 241, Level 16, State 1, Line 71
-- Conversion failed when converting date and/or time from character string.
GO

SELECT @@LANGUAGE, CONVERT(DATE, '13/5/2016', 103) AS [Test 2]; -- 103 = dd/mm/yyyy
-- us_english   2016-05-13
GO


IF (@@LANGID <> 2) -- Français
BEGIN
    PRINT 'Changing LANGUAGE to French...';
    SET LANGUAGE FRENCH;
    SELECT @@LANGUAGE AS [CurrentLanguage], @@LANGID AS [LangID];
END;

SELECT @@LANGUAGE, CAST('13/5/2016' AS DATE) AS [Test 3];
-- 2016-05-13
GO

SELECT @@LANGUAGE, CONVERT(DATE, '13/5/2016', 103) AS [Test 4]; -- 103 = dd/mm/yyyy
-- Franais 2016-05-13
GO


-- Reset current language, if necessary.
IF (@@LANGID <> @@DEFAULT_LANGID)
BEGIN
    DECLARE @Language sysname;

    SELECT @Language = sl.[alias]
    FROM   sys.syslanguages sl
    WHERE  sl.[langid] = @@DEFAULT_LANGID;

    PRINT N'Changing LANGUAGE back to default: ' + @Language + N'...';

    SET LANGUAGE @Language;
    SELECT @@LANGUAGE AS [CurrentLanguage], @@LANGID AS [LangID];
END;
2 of 3
4

I'm not aware of any 'performance' differences between the two. Apparently "CONVERT" is Sql Server specific while CAST is ANSI-standard. I believe CONVERT gives you more options. You can see other pros/cons here (http://searchsqlserver.techtarget.com/tip/The-difference-between-CONVERT-and-CAST-in-SQL-Server)

Direct quotes from link...

Because SQL Server provides both functions , there may be some confusion about which is best to use and under what circumstances.

CONVERT is specific to SQL Server, and allows for a greater breadth of flexibility when converting between date and time values, fractional numbers, and monetary signifiers.

CAST is the more ANSI-standard of the two functions, meaning that while it's more portable (i.e., a function that uses CAST can be used in other database applications more or less as-is), it's also less powerful.

🌐
SQLServerCentral
sqlservercentral.com › forums › topic › using-cast-or-convert-to-change-data-from-string-to-datetime
Using CAST or CONVERT to change data from string to datetime – SQLServerCentral Forums
April 16, 2014 - It works for input too, describing the shape of a text string for a string-datatype to date-datatype conversion. Your data corresponds to style 12: yymmdd. Hence, all you need to convert your data is this:
🌐
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
🌐
MSSQLTips
mssqltips.com › home › sql date format examples using convert function
SQL Date Format Examples using SQL CONVERT Function
September 26, 2025 - DECLARE @Datetime DATETIME; SET @Datetime = GETDATE(); --dd/mm/yyyy with 4 DIGIT YEAR SELECT CONVERT(VARCHAR(10), @Datetime, 103) CurrentDateFormattedAsText; --dd/mm/yy with 2 DIGIT YEAR SELECT CONVERT(VARCHAR(8), @Datetime, 3) CurrentDateFormattedAsText; -- pull data from a database table -- The date used for this example was January 25, 2013. --SELECT a datetime column as a string formatted dd/mm/yyyy (4 digit year) SELECT TOP 3 CONVERT(CHAR(10), ExpectedDeliveryDate, 103) ExpectedDeliveryDateFormattedAsText FROM Purchasing.PurchaseOrders WHERE OrderDate < @Datetime; --SELECT a datetime column as a string formatted dd/mm/yy (2 digit year) SELECT TOP 3 CONVERT(CHAR(8), ExpectedDeliveryDate, 3) ExpectedDeliveryDateFormattedAsText FROM Purchasing.PurchaseOrders WHERE OrderDate < @Datetime;
🌐
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 more information, see Nondeterministic conversion of literal date strings into DATE values. Starting with SQL Server 2012 (11.x), when using supplementary character (SC) collations, a CAST operation from nchar or nvarchar to an nchar or nvarchar type of smaller length won't truncate inside a surrogate pair. Instead, the operation truncates before the supplementary character. For example, the following code fragment leaves @x holding just 'ab'. There isn't enough space to hold the supplementary character.
🌐
Snowflake Documentation
docs.snowflake.com › en › sql-reference › functions › to_date
TO_DATE , DATE | Snowflake Documentation
The display format for dates in the output is determined by the DATE_OUTPUT_FORMAT session parameter (default YYYY-MM-DD). If the format of the input parameter is a string that contains an integer: After the string is converted to an integer, the integer is treated as a number of seconds, milliseconds, microseconds, or nanoseconds after the start of the Unix epoch (1970-01-01 00:00:00.000000000 UTC).
🌐
GeeksforGeeks
geeksforgeeks.org › sql › sql-query-to-convert-datetime-to-date
SQL Query to Convert DateTime to Date in SQL Server - GeeksforGeeks
SELECT CONVERT(DATE, '2021-08-27 17:26:36.710' ) AS CURRENT_DATE_GFG ... Explanation: The CONVERT() function efficiently converts the string DateTime to a Date, again discarding the time portion.
Published   July 23, 2025