Date String Conversion in SQL Server: Correct Approach from '23/07/2009' to datetime

Nov 20, 2025 · Programming · 13 views · 7.8

Keywords: SQL Server | Date Conversion | CONVERT Function | datetime | String Format

Abstract: This article provides an in-depth exploration of the CONVERT function's application in date string conversion within SQL Server, focusing on the distinction between style parameters 103 and 111. Through concrete examples, it demonstrates how to properly convert 'dd/mm/yyyy' formatted strings to datetime type, avoiding common 'out-of-range value' errors, and offers a comprehensive reference table of date-time styles along with usage recommendations.

Problem Background and Error Analysis

In SQL Server development, format conversion of date strings is a common yet error-prone operation. When users attempt CONVERT(datetime, '23/07/2009', 111), they encounter the error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value," whereas CONVERT(datetime, '07/23/2009', 111) executes successfully.

Core Role of Style Parameter

The third parameter of the CONVERT function—style—determines the parsing format of the string. According to Microsoft official documentation, style 111 corresponds to the Japanese standard format yyyy/mm/dd, meaning the string must be arranged in "year/month/day" order. Thus, '07/23/2009' is correctly parsed as July 23, 2009, while '23/07/2009' fails conversion due to format mismatch.

Correct Solution

For date strings in dd/mm/yyyy format, style 103 should be used, which corresponds to the British/French standard format:

SELECT CONVERT(datetime, '23/07/2009', 103)

This code will successfully convert the string to datetime type, resulting in July 23, 2009.

Detailed Explanation of Date-Time Styles

SQL Server offers a rich set of date-time conversion styles. Here are some commonly used styles and their corresponding formats:

Comparison of CONVERT and CAST Functions

SQL Server provides two type conversion functions:

The advantage of the CONVERT function lies in its support for style parameters, enabling handling of specific date-time string formats, whereas the CAST function is suitable for simple data type conversions.

Practical Application Examples

The following examples demonstrate conversion methods for date strings in different formats:

-- U.S. format conversion
SELECT CONVERT(datetime, '07/23/2009', 101) AS US_Format

-- ISO format conversion  
SELECT CONVERT(datetime, '20090723', 112) AS ISO_Format

-- German format conversion
SELECT CONVERT(datetime, '23.07.2009', 104) AS German_Format

Error Prevention and Best Practices

To avoid date conversion errors, it is recommended to:

  1. Always explicitly specify the style parameter to avoid relying on default settings
  2. Use four-digit years to prevent ambiguity with two-digit years
  3. Perform thorough format validation in production environments
  4. Consider using TRY_CONVERT function for error handling

Extended Application Scenarios

Date-time conversion is not limited to string-to-datetime conversion but also includes:

By mastering the usage of style parameters in the CONVERT function, developers can efficiently handle various international date formats, ensuring global compatibility of database applications.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.