How to Convert a String to a Date/Time in SQL Server using CAST()

In SQL Server, you can use the CAST() function to convert an expression of one data type to another.  This function works almost the same as the CONVERT() function, except that the syntax is slightly different (and CAST() doesn’t accept the style argument).

So if you need to convert a string to a date/time value, you can use the CONVERT() function or the CAST() function.

And if you get an error while using those two functions, the PARSE() function might be just what you need.

This article contains examples using the CAST() function.

Syntax

The syntax goes like this:

CAST ( expression AS data_type [ ( length ) ] )

These arguments are defined as follows:

expression
Any valid expression.
data_type
The target data type. This includes xmlbigint, and sql_variant. Alias data types cannot be used.
length
An optional integer that specifies the length of the target data type. The default value is 30.

Example 1 – Convert to date Data Type

In this example, we convert the string into a date data type (we specify this by using date as the second argument).

SELECT CAST('20 Dec 2018' AS date) AS Result;

Result:

+------------+
| Result     |
|------------|
| 2018-12-20 |
+------------+

Just like with CONVERT() the string needs to be in a style that’s able to be converted to the specified data type, otherwise you’ll get an error:

SELECT CAST('Homer' AS date) AS Result;

Result:

Conversion failed when converting date and/or time from character string.

Example 2 – Convert to datetime Data Type

In this example we convert to the datetime data type:

SELECT CAST('20 Dec 2018' AS datetime) AS Result;

Result:

+-------------------------+
| Result                  |
|-------------------------|
| 2018-12-20 00:00:00.000 |
+-------------------------+

You can also specify the time along with the date:

SELECT CAST('2pm 20 Dec 2018' AS datetime) AS Result;

Result:

+-------------------------+
| Result                  |
|-------------------------|
| 2018-12-20 14:00:00.000 |
+-------------------------+

Example 3 – Convert to datetime2 Data Type

Here we convert to the datetime2 data type:

SELECT CAST('20 Dec 2018' AS datetime2) AS Result;

Result:

+-----------------------------+
| Result                      |
|-----------------------------|
| 2018-12-20 00:00:00.0000000 |
+-----------------------------+

Example 4 – Convert to datetimeoffset Data Type

In this example we convert to the datetimeoffset data type:

SELECT CAST('20 Dec 2018' AS datetimeoffset) AS Result;

Result:

+------------------------------------+
| Result                             |
|------------------------------------|
| 2018-12-20 00:00:00.0000000 +00:00 |
+------------------------------------+

Example 5 – Convert to smalldatetime Data Type

Here we convert to the smalldatetime data type:

SELECT CAST('20 Dec 2018' AS smalldatetime) AS Result;

Result:

+---------------------+
| Result              |
|---------------------|
| 2018-12-20 00:00:00 |
+---------------------+

Example 6 – Convert to time Data Type

In this example we convert to the time data type:

SELECT CAST('20 Dec 2018' AS time) AS Result;

Result:

+----------+
| Result   |
|----------|
| 00:00:00 |
+----------+

In that case, we specified a date but not a time, so it came back as 00:00:00. Here’s another example where we do actually specify a time:

SELECT CAST('2pm' AS time) AS Result;

Result:

+----------+
| Result   |
|----------|
| 14:00:00 |
+----------+

But if we add the date, we get an error (unlike an earlier example when we converted to datetime):

SELECT CAST('2pm 20 Dec 2018' AS time) AS Result;

Result:

Conversion failed when converting date and/or time from character string.

So in this case you’d need to convert to datetime or smalldatetime.

Example 7 – Omitting the Date

If you omit the date while converting to the datetime or smalldatetime data types, it will return 1900-01-01 as the date:

SELECT CAST('2pm' AS datetime) AS Result;

Result:

+-------------------------+
| Result                  |
|-------------------------|
| 1900-01-01 14:00:00.000 |
+-------------------------+

If you prefer to use to use the CONVERT() function, see these string to date/time CONVERT() examples using the same examples as above.

Also, as mentioned, if you get an error while trying to convert a string to a date, try the PARSE() function instead. It will work in some cases where CAST() doesn’t.