93

How can I convert a String to a Date in T-SQL?

My test case is the string: '24.04.2012'

6 Answers 6

146
CONVERT(datetime, '24.04.2012', 104)

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

0
28

Microsoft SQL Date Formats

CONVERT(DateTime, DateField, 104)
27

Assuming that the database is MS SQL Server 2012 or greater, here's a solution that works. The basic statement contains the in-line try-parse:

SELECT TRY_PARSE('02/04/2016 10:52:00' AS datetime USING 'en-US') AS Result;

Here's what we implemented in the production version:

UPDATE dbo.StagingInputReview
 SET ReviewedOn = 
     ISNULL(TRY_PARSE(RTrim(LTrim(ReviewedOnText)) AS datetime USING 'en-US'), getdate()),
 ModifiedOn = (getdate()), ModifiedBy = (suser_sname())
 -- Check for empty/null/'NULL' text
 WHERE not ReviewedOnText is null 
   AND RTrim(LTrim(ReviewedOnText))<>''
   AND Replace(RTrim(LTrim(ReviewedOnText)),'''','') <> 'NULL';

The ModifiedOn and ModifiedBy columns are just for internal database tracking purposes.

See also these Microsoft MSDN references:

1
  • This assumes that the OP is using Sql Server 2012+
    – Mark Kram
    Sep 30, 2019 at 20:14
9

Although the CONVERT thing works, you actually shouldn't use it. You should ask yourself why you are parsing string values in SQL-Server. If this is a one-time job where you are manually fixing some data you won't get that data another time, this is ok, but if any application is using this, you should change something. Best way would be to use the "date" data type. If this is user input, this is even worse. Then you should first do some checking in the client. If you really want to pass string values where SQL-Server expects a date, you can always use ISO format ('YYYYMMDD') and it should convert automatically.

3
  • 5
    What about a situation where you're importing data files from some external system, and the input column happens to be in one of these formats, e.g. "05/31/2013", but which comes in as a string ? And you're writing, say, a stored procedure to import that data, or using SSIS to import it? Then CONVERT would be the appropriate thing to use, would it not? Nov 18, 2013 at 13:32
  • Not necessarily, in the situations where MS SQL Server host analytic data models, as opposed to transaction ones, it is perfectly okay to use CONVERT because it has to deal with a lot of external sources that would not be readily coming in as a date type (as David Barrows alluded to above).
    – Will
    Nov 4, 2014 at 19:00
  • Default string literal format for dates is YYYY-MM-DD
    – Y.B.
    Feb 1, 2018 at 11:49
5

You can use:

SELECT CONVERT(datetime, '24.04.2012', 103) AS Date

Reference: CAST and CONVERT (Transact-SQL)

1
  • 2
    It's interesting that works. Format 103 is dd/mm/yyyy and 104 is dd.mm.yyyy.
    – Jeff Moden
    Jun 6, 2020 at 16:26
1
CONVERT(DateTime, ExpireDate, 121) AS ExpireDate

will do what is needed, result:

2012-04-24 00:00:00.000
1
  • That doesn't appear to work for what the OP posted even for different language settings. Try it. If it actually does work for you, then please post what your current language setting is. Thanks. Here's the code to try... SELECT CONVERT(DateTime, '24.04.2012', 121) AS ExpireDate
    – Jeff Moden
    Jun 6, 2020 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.