Convert Datetime to Date

Summary: in this tutorial, you will learn how to convert a datetime to a DATE by using the CONVERT(), TRY_CONVERT(), and CAST() functions.

To convert a datetime to a date, you can use the CONVERT(), TRY_CONVERT(), or CAST() function.

Convert datetime to date using the CONVERT() function

This statement uses the CONVERT() function to convert a datetime to a date:

CONVERT(DATE, datetime_expression)
Code language: SQL (Structured Query Language) (sql)

In this syntax, the datetime_expresssion is any valid expression that evaluates to a valid datetime value. The CONVERT() function will raise an error if the conversion fails.

The following example uses the CONVERT() function to convert a datetime to a date:

SELECT 
    CONVERT(DATE, GETDATE()) date;
Code language: SQL (Structured Query Language) (sql)

Here is the output:

date
----------
2019-04-23

(1 row affected)
Code language: SQL (Structured Query Language) (sql)

Note that the GETDATE() function returns the current database server’s datetime.

Convert datetime to date using the TRY_CONVERT() function

Similarly, the TRY_CONVERT() can also be used to convert the datetime to a date:

TRY_CONVERT(DATE, datetime_expression)
Code language: SQL (Structured Query Language) (sql)

Unlike the CONVERT() function, the TRY_CONVERT() function returns NULL if the conversion fails.

This example uses the TRY_CONVERT() function to convert the current datetime to a date:

SELECT
    TRY_CONVERT(DATE,GETDATE());
Code language: SQL (Structured Query Language) (sql)

The following shows the output:

date
----------
2019-04-23

(1 row affected)
Code language: SQL (Structured Query Language) (sql)

Convert datetime to date using the CAST() function

The following statement converts a datetime value to a date using the CAST() function:

CAST(datetime_expression AS DATE)
Code language: SQL (Structured Query Language) (sql)

This example uses the CAST() function to convert the current datetime to a date value:

SELECT 
    CAST(GETDATE() AS DATE) date;
Code language: SQL (Structured Query Language) (sql)

The output is as follows:

date
----------
2019-04-23

(1 row affected)
Code language: SQL (Structured Query Language) (sql)

In this tutorial, you have learned how to use how to convert a datetime to a date using the CONVERT(), TRY_CONVERT(), and CAST() functions.

Was this tutorial helpful?