2

Is it possible to convert datetime from SQL statements ?

Example :

SELECT * FROM t_status WHERE Function to Convert(date_added) BETWEEN '2013-05-01' AND '2013-05-31'

My date time format in table t_status like this :

2013-05-20, 18:00 <-- contains time.

Any advice ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Interval Grid
  • 187
  • 3
  • 14

2 Answers2

2

You can convert string to date in MySQL using the STR_TO_DATE function.

The date in your sample (assuming the comma is intentional) can be converted with the format string '%Y-%m-%d, %H:%i':

WHERE STR_TO_DATE(date_added, '%Y-%m-%d, %H:%i') BETWEEN '2013-05-01' AND '2013-05-31'

Or to ignore the time part, do this:

WHERE STR_TO_DATE(date_added, '%Y-%m-%d') BETWEEN '2013-05-01' AND '2013-05-31'
Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69
0
WHERE DATE(date_added) BETWEEN '2013-05-01' AND '2013-05-31'

from the manual - DATE(expr) : Extracts the date part of the date or datetime expression expr.

Next time please try searching Stackoverflow first as there are numerous questions about this already - https://stackoverflow.com/search?q=mysql+select+date+from+datetime

eg.
datetime mySQL SELECT only date
How to select date from datetime column?
How to cast DATETIME as a DATE in mysql?

Community
  • 1
  • 1
Sean
  • 12,443
  • 3
  • 29
  • 47