Example
Convert a string representation and format into a valid date.
SELECT STR_TO_DATE(“August 10 2017”, “%M %d %Y”); |
Definition and Usage
The STR_TO_DATE() function converts a string representation and a specified format into a date.
Syntax
STR_TO_DATE(string, format) |
Parameter Values
Parameter
|
Description
|
string
|
Mandatory: The string that needs to be converted into a date format.
|
format
|
Mandatory: The format specification to apply. It can consist of one or a combination of the following values:
Format
|
Description
|
%a
|
Shortened day of the week name (Sun to Sat)
|
%b
|
Shortened month name (Jan to Dec)
|
%c
|
Numeric representation of a month (0 to 12)
|
%D
|
Numeric day of the month followed by its ordinal suffix (1st, 2nd, 3rd, …)
|
%d
|
Numeric day of the month (01 to 31)
|
%e
|
Numeric day of the month (0 to 31)
|
%f
|
Microseconds (000000 to 999999)
|
%H
|
Hour (00 to 23)
|
%h
|
Hour (00 to 12)
|
%I
|
Hour (00 to 12)
|
%i
|
Minutes (00 to 59)
|
%j
|
Day of the year (001 to 366) represented
|
%k
|
Hour (0 to 23)
|
%l
|
Hour (1 to 12) represented
|
%M
|
Full month name (January to December)
|
%m
|
Numeric month value (01 to 12)
|
%p
|
AM or PM expressed
|
%r
|
Time in 12-hour AM/PM format (hh:mm
AM/PM)
|
%S
|
seconds (from 00 to 59)
|
%s
|
Seconds (00 through 59)
|
%T
|
Time expressed in the 24-hour format (hh:mm)
|
%U
|
Weeks in which Sunday is considered the first day (00 to 53)
|
%u
|
Weeks with Monday as the first day of the week (00 to 53)
|
%V
|
Weeks starting with Sunday as the first day (01 to 53), used with %X
|
%v
|
Weeks starting with Monday as the first day (01 to 53), applicable with %X
|
%W
|
Full name of the weekday (Sunday to Saturday)
|
%w
|
Day of the week with Sunday=0 and Saturday=6
|
%X
|
Year corresponding to the week where Sunday is the first day, used with %V
|
%x
|
Year corresponding to the week where Monday is the first day, used with %V
|
%Y
|
Year represented as a 4-digit numeric value
|
%y
|
Year represented as a 2-digit numeric value
|
|
Technical Details
Works in:
|
From MySQL version 4.0
|
More Examples
Example
Convert a string into a date using a specified format.
SELECT STR_TO_DATE(“August,5,2017”, “%M %e %Y”); |
Example
Produce a date from a given string using a specified format.
SELECT STR_TO_DATE(“Monday, August 14, 2017”, “%W %M %e %Y”); |
Example
Generate a date from a string using a specified format.
SELECT STR_TO_DATE(“2017,8,14 10,40,10”, “%Y,%m,%d %h,%i,%s”); |