Open In App

STR_TO_DATE() function in MySQL

Last Updated : 22 Dec, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
STR_TO_DATE() : This 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-1 : 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
Example-2 : Converting '1900' to date value having '%Y' format using STR_TO_DATE() function.
SELECT STR_TO_DATE('1900', '%Y') 
As New_form;
Output :
New_form
1900-00-00
Example-3 : Converting '111111' to time value having '%h%i%s' format using STR_TO_DATE() function

SELECT STR_TO_DATE('111111', '%h%i%s') 
As New_form;
Output :
New_form
11:11:11
Example-4 : Passing an empty string as an argument of STR_TO_DATE() function.
SELECT STR_TO_DATE('', '%h') 
As New_form;
Output :
New_form
00:00:00
Example-5 : Converting '20100212 103545' to DateTime value having '%Y%m%d %h%i%s' format using STR_TO_DATE() function.
SELECT STR_TO_DATE('20100212 103545', '%Y%m%d %h%i%s') 
As New_form;
Output :
New_form
2010-02-12 10:35:45

Next Article

Similar Reads