I'm trying to format date using MySQL DATE_FORMAT. My Query is
SELECT first_name, middle_name, last_name, DATE_FORMAT(added_date,'%d/%m/%Y') AS AddDate
FROM profiles.
added_date is the date field on profile table, when I run that query it brings Null for AddDate, any suggestion.
Thanks.
DATE_FORMAT() function in MySQL is used to format a specified date as given format value i.e., a date will be given and this function will format that date as specified format parameters. Syntax : DATE_FORMAT(date, format)
The following is the output. The following is the query to format the date to YYYY-MM-DD. mysql> select str_to_date(LoginDate,'%d. %m.
Introduction to MySQL DATE data type 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. MySQL uses 3 bytes to store a DATE value. The DATE values range from 1000-01-01 to 9999-12-31 .
To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days.
Based on your comments you are probably looking for this
SELECT first_name, middle_name, last_name,
DATE_FORMAT(STR_TO_DATE(added_date, '%d-%m-%Y %h:%i:%s %p'),'%d/%m/%Y') AS AddDate
FROM profiles
Sample output:
| FIRST_NAME | MIDDLE_NAME | LAST_NAME | ADDDATE | ----------------------------------------------------- | Jhon | (null) | Doe | 11/08/2013 |
Here is SQLFiddle demo
If you're storing it as an INT you might need to use FROM_UNIXTIME()
SELECT
first_name,
middle_name,
last_name,
DATE_FORMAT(FROM_UNIXTIME(added_date),'%d/%m/%Y') AS AddDate
FROM
profiles
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With