Explain STR_TO_DATE() Function in SQL?

STR_TO_DATE(): function in MySQL helps to convert string values to date or time or DateTime values. The function will return zero (0000-00-00) if an empty string is passed as an argument.

Syntax :

STR_TO_DATE(string, format)

Parameters :

  • string –The string which will be converted to DateTime.
  • format –The format in which it will be converted.

Return :

  • The function will return the DateTime value of the given string in a specified format.
  • The function will return zero (0000-00-00) if an empty string is passed as an argument.

Example: Converting ’21, 7, 2023′ to date value having ‘%d, %m, %Y’ format using STR_TO_DATE() function.

SELECT STR_TO_DATE('21, 07, 2023', '%d, %m, %Y') As New_form;

Output :

New_form

2023-07-21