Search code examples
mysqldatestr-to-date

str_to_date in mysql for a string


I am getting the $date value as string. How to convert it into date format

I am getting $date='2015-12-01' i.e, $date in the format 'yyyy-mm-dd'. I want to convert it into date format using str_to_date like year-month-date

i.e $date='2015-12-01' must be converted to date format $date=2015-12-01 in mysql query.

What is the date format that I have to give?


Solution

  • use str_to_date

    str_to_date('2015-12-01','%Y-%m-%d')
    

    other option

    CAST('2015-12-01' AS DATETIME)
    

    but if your date format is '%Y-%m-%d' or '2015-12-01' MySQL auto recognizes as dates