Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to convert from datetime to date mysql

I am trying to get the date portion of a datetime field. I know I can get it with date_format, but that returns a string or "varchar" field. How can I convert the result to date and not as varchar?

This is my query returning the varchar:

(Select Date_Format(orders.date_purchased,'%m/%d/%Y')) As Date   

I tried several combinations from this question, but could not make it to work:

mysql query - format date on output?

Any help is appreciated.

like image 629
chupeman Avatar asked Jan 19 '11 20:01

chupeman


People also ask

How convert date format from DD MM YYYY to Yyyymmdd in MySQL?

Use STR_TO_DATE() method from MySQL to convert. The syntax is as follows wherein we are using format specifiers. The format specifiers begin with %. SELECT STR_TO_DATE(yourDateColumnName,'%d.

How do I convert timestamps to dates?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

How can get date in dd mm yyyy format in MySQL?

The MySQL DATE_FORMAT() function formats a date value with a given specified format. You may also use MySQL DATE_FORMAT() on datetime values and use some of the formats specified for the TIME_FORMAT() function to format the time value as well.

How do I change the date format in MySQL?

MySQL uses yyyy-mm-dd format for storing a date value. This format is fixed and it is not possible to change it. For example, you may prefer to use mm-dd-yyyy format but you can't. Instead, you follow the standard date format and use the DATE_FORMAT function to format the date the way you want.


1 Answers

Try to cast it as a DATE

SELECT CAST(orders.date_purchased AS DATE) AS DATE_PURCHASED 
like image 90
Chandu Avatar answered Sep 30 '22 09:09

Chandu