Convert DateTime Value into String in MySQL?


To convert the DateTime value into string in MySQL, you can use the DATE_FORMAT() function. The syntax is as follows −

select date_format(yourColumnName, ‘%d %m %y’) as anyVariableName from yourTableName;

To understand the above concept, let us create a table. The query to create a table is as follows −

mysql> create table DateAsStringDemo
   -> (
   -> YourDateTime datetime
   -> );
Query OK, 0 rows affected (0.57 sec)

Inserting the date with the help of curdate() method. The query to insert date is as follows −

mysql> insert into DateAsStringDemo values(curdate());
Query OK, 1 row affected (0.24 sec)

Let us display all records with the help of select statement. The query is as follows −

mysql> select *from DateAsStringDemo;

The following is the output −

+---------------------+
| YourDateTime        |
+---------------------+
| 2018-11-26 00:00:00 |
+---------------------+
1 row in set (0.00 sec)

The query to convert date to string is as follows −

mysql> select date_format(YourDateTime,'%d %m %y') as YourDateAsString from DateAsStringDemo;

The following is the output −

+------------------+
| YourDateAsString |
+------------------+
| 26 11 18         |
+------------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements