The following shows the TO_DATE() function syntax: TO_DATE(string, format)įor example, to convert the string '' to a date value, you use the following statement: SELECT TO_DATE( '', 'DD MON YYYY' ) Oracle and PostgreSQL provide the TO_DATE() function that converts a string to date based on a specified format. Here is the result: Code language: SQL (Structured Query Language) ( sql ) Convert string to date using TO_DATE() function Similarly, the following example also converts the string 180101 to January 1st, 2018: SELECT CAST( '180101' AS DATE) SQL Server converts the string 2018 to January 1st, 2018:
The following example shows how to convert a string to a date: SELECT CAST( '2018' AS DATE)
In case the function fails to convert, it issues an error, depending on the implementation of a specific database system. The CAST() function returns a DATE value if it successfully converts the string to date. In this syntax, the string can be any DATE value that is convertible to a date. The following illustrates the syntax of the CAST() function: CAST (string AS DATE)Ĭode language: SQL (Structured Query Language) ( sql ) SQL provides a CAST() function that allows you to convert a string to a date. Convert string to date using CAST() function Summary: in this tutorial, you will learn various functions that convert a string to a date in SQL.