The MySQL STR_TO_DATE() function allows you to build a date value from the various date parts.
When using this function, you provide a string of the various date parts, and a second argument that specifies the format that the date is provided in.
Perhaps you’ve encountered the T-SQL PARSE(), CAST(), and CONVERT() functions when working with SQL Server and wondered what the difference is. All three functions seem to do the same thing, but there are subtle differences between them.
In this article I aim to outline the main differences between these functions.
In SQL Server, the PARSE() and TRY_PARSE() functions are used for translating a value into another data type. They essentially do the same thing, with one exception; how they deal with errors.
If PARSE() fails when attempting to parsing to a different data type, it will return an error. If TRY_PARSE() fails, it will return NULL.
If you need to convert a string into a date/time value in SQL Server, you have a number of options. In this post I outline six T-SQL functions that allow you to do this.
The six functions are:
CAST()CONVERT()PARSE()TRY_CAST()TRY_CONVERT()TRY_PARSE()Below are example of how you can use these functions to convert a string to a date/time data type.
If you work with SQL Server, chances are you’ve used at least one of the CONVERT() or CAST() functions to convert from one data type to another. If you’ve ever encountered an error while trying to convert a string to a date/time data type, the PARSE() function could be what you need.
For example, if you have a string like say, Fri, 20 Jul 2018, the CONVERT() or CAST() functions will throw an error. But the PARSE() function will handle it without a problem.
The PARSE() function returns the result of an expression, translated to the requested data type in SQL Server. So you can use it to “translate” your string value into a date/time data type (such as date, datetime, datetime2, etc).
When using SQL Server, you can convert a date/time value into a string by using the CONVERT() function. This function allows you to convert between different data types.
In this article, we’ll be converting between various date/time data types to a varchar or nvarchar string.
One of the good things about this function is that it allows you to specify the style that the date will be returned in. For example, you can specify whether it’s returned as mm/dd/yyyy, yyyy.mm.dd, Mon dd, yyyy, etc. You can also specify whether the time component is returned and how it’s styled.
The CONVERT() function allows you to convert between data types. It’s similar to the CAST() function, but one of the benefits of CONVERT() is that, when you convert from a date/time data type to a string, you can add an optional argument that specifies the style that you want the return value to be in. For example, you can have it returned as dd.mm.yyyy, yyyy-mm-dd, dd mon yyyy, etc
This article contains examples of the various styles you can return when converting a date/time value to a string using the CONVERT() function in SQL Server.
In SQL Server, you can use use the CONVERT() function to convert a date value from one date data type to another (or between any other data type for that matter). However, that’s not the only function in the T-SQL toolbox for converting between data types.
The CAST() function is part of the ANSI SQL standard, and it does most of the things CONVERT() does. So in many cases, you have the option of which of these functions you prefer to use.
Many database professionals prefer CAST() due to the fact that it’s part of the ANSI SQL standard, however, others prefer CONVERT() due to the extra functionality that T-SQL’s implementation offers (such as being able to provide a date style).
In any case, this article provides examples of converting between different date formats using CAST().