Software Development

What is the most reliable way to convert various string formats to Date in SQL Server (T-SQL)?

SA Asked by Sandra Robinson · 15-08-2025
0 upvotes 17,328 views 0 comments
The question

I am dealing with a legacy database where dates are stored as strings in multiple formats like 'DD/MM/YYYY', 'MM-DD-YYYY', and 'YYYYMMDD'. When I try to cast them to a DATE type, I often get "Conversion failed when converting date and/or time from character string" errors. What are the best T-SQL functions to handle this, and how can I safely manage different regional date settings without crashing my queries?

3 answers

0
DO
Answered on 17-08-2025

The most robust method in SQL Server is the CONVERT() function because it allows you to specify a style code that matches your input format. For example, CONVERT(DATE, '31/12/2023', 103) handles the British/French format perfectly. If you are using SQL Server 2012 or later, I highly recommend using TRY_CONVERT() or TRY_CAST(). These functions return a NULL value instead of a high-severity error if the conversion fails, which prevents your entire batch or report from crashing due to a single malformed row. For standard 'YYYYMMDD' formats, SQL Server usually handles these implicitly as they are ISO-standard, but being explicit with style 112 is always safer for long-term maintenance.

0
MA
Answered on 19-08-2025

Using TRY_CONVERT is a lifesaver for data cleaning, but if I have a mix of formats in the same column, is there a way to validate the format before attempting the conversion?

CH 21-08-2025

You can use a CASE statement combined with ISDATE() or TRY_CONVERT(). For instance, check if TRY_CONVERT(DATE, column, 101) is not null for US format, else try style 103. However, ISDATE() can be tricky because it depends on your server's LANGUAGE and DATEFORMAT settings. A more modern approach is using TRY_PARSE() with the USING culture clause, like TRY_PARSE('12/05/2023' AS DATE USING 'en-GB'), which is very explicit and ignores server-wide regional settings.

0
LI
Answered on 10-09-2025

I always use CAST(string AS DATE) for ISO formats like 'YYYY-MM-DD'. It’s simple, ANSI-standard, and works across most SQL dialects without needing style codes.

SA 12-09-2025

I agree with Lisa for standard formats, but just be careful with CAST if your strings are 'MM/DD/YYYY', as it can fail depending on the SET DATEFORMAT of the session. CONVERT is usually my "safety first" choice.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session