![]() 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 In this tutorial, we have shown you various examples of using the MySQL STR_TO_DATE() function to convert strings to date and time values.Summary: in this tutorial, you will learn various functions that convert a string to a date in SQL. SELECT STR_TO_DATE( '20130101 1130', '%Y%m%d %h%i') Code language: SQL (Structured Query Language) ( sql ) The following example converts the string into a DATETIME value because the input string provides both date and time parts. Similar to the unspecified date part, the STR_TO_DATE() function sets unspecified time part to zero, see the following example: SELECT STR_TO_DATE( '11', '%h') Code language: SQL (Structured Query Language) ( sql ) The following example converts a time string into a TIME value: SELECT STR_TO_DATE( '113005', '%h%i%s') Code language: SQL (Structured Query Language) ( sql ) ![]() See the following example: SELECT STR_TO_DATE( '2013', '%Y') Code language: SQL (Structured Query Language) ( sql )īecause the input string only provides year value, the STR_TO_DATE() function returns a date value that has month and day set to zero. The STR_TO_DATE() sets all incomplete date values, which are not provided by the input string, to zero. ![]() See the following example: SELECT STR_TO_DATE( '21,5,2013 extra characters', '%d,%m,%Y') Code language: SQL (Structured Query Language) ( sql ) The STR_TO_DATE() function ignores extra characters at the end of the input string when it parses the input string based on the format string.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |