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 ?

2 Answers 2

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'
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.