Published on

January 29, 2020

Converting String to Date in SQL Server

When working with raw data in SQL Server, you may come across date values stored as text. Converting these values to a date data type is crucial for accurate analysis. In SQL Server, there are different approaches to converting a string to a date. In this article, we will explore the implicit and explicit conversion methods using built-in functions such as CAST(), TRY_CAST(), CONVERT(), TRY_CONVERT(), and TRY_PARSE().

Implicit Conversion

Implicit conversion is not visible to the user and occurs when comparing fields or values with different data types. In SQL Server, converting a string to a date implicitly depends on the string date format and the default language settings. If the date stored within a string is in ISO formats (yyyyMMdd or yyyy-MM-ddTHH:mm:ss(.mmm)), it can be converted regardless of the regional settings. However, if the date has a different format, it must be a supported format or it will throw an exception.

For example, if we try to convert a string with the format dd/MM/yyyy while working under the regional settings “EN-US”, it will fail since it tries to convert it as MM/dd/yyyy format which is not supported.

Explicit Conversion

The explicit conversion method involves using functions such as CAST(), CONVERT(), and PARSE().

CAST()

The CAST() function is the most basic conversion function provided by SQL Server. It tries to convert a given value to a specified data type. However, the success of converting a string to a date using CAST() function depends on the language settings, similar to implicit conversion. Only ISO formats or supported formats by the current language settings can be converted.

CONVERT()

The CONVERT() function is more advanced than CAST() as it allows specifying the conversion style. This function takes three arguments: the desired data type, the input value, and the style number (optional). If the style number is not passed, it acts like the CAST() function. By specifying the style number, the function will try to convert the value based on that style.

PARSE()

The PARSE() function is a SQL CLR function that uses the .NET framework Parse() function. It can be used to convert a value to the desired data type using a specified culture. If the culture info is not specified, PARSE() acts similar to the CAST() function. When the culture is passed, the function tries to convert the value using that culture.

TRY_CAST(), TRY_CONVERT(), and TRY_PARSE()

One issue with data type conversion functions is that they cannot handle erroneous values. To handle this, SQL Server provides TRY_CAST(), TRY_CONVERT(), and TRY_PARSE() functions. These functions check if the value can be converted and return the conversion result if successful. Otherwise, they return a NULL value.

Conclusion

In this article, we discussed the different approaches to converting a string to a date in SQL Server. We explored the implicit and explicit conversion methods using functions such as CAST(), CONVERT(), and PARSE(). We also learned about the TRY_CAST(), TRY_CONVERT(), and TRY_PARSE() functions for handling erroneous values. Understanding these conversion methods is essential for working with date values effectively in SQL Server.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.