Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL DATE_FORMAT()

Tags:

date

mysql

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.

like image 319
user1554459 Avatar asked Aug 11 '13 07:08

user1554459


People also ask

What is DATE_FORMAT in MySQL?

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)

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

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.

How do I format a date field in MySQL?

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 .

How can I get date between two dates in MySQL?

To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days.


2 Answers

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

like image 60
peterm Avatar answered Nov 15 '22 06:11

peterm


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
like image 21
Jared Avatar answered Nov 15 '22 06:11

Jared