Question

I have a datecreated field in my database which is of type varchar. Unfortunately I cannot change this field to a date format, so I have to do it when selecting the data. I am trying to sort the records according to date (so that the latest year then month then day then hour then minute then second is displayed). The field format looks like this dd-mm-yyyy hh:mm:ss , so in my query, I am trying the following, however it does absolutely no ordering at all :

SELECT * FROM submissions ORDER BY str_to_date(datecreated,'%d-%m-%Y %H:%i:%s') DESC

All it does is displays the records according to the id field...

Was it helpful?

Solution

Assuming all of the datecreated values match the format you specified, that query should work.

Here's an example that shows your query working properly in MySQL 5.6.14 using 10 random dates spread across the past 4 years:

Setup:

mysql> create table submissions (
    -> id int not null auto_increment primary key,
    -> datecreated varchar(20)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> 
mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into submissions (datecreated) select date_format(now() - interval (rand() * 31536000 * 4) second,'%d-%m-%Y %H:%i:%s');
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

Query:

mysql> SELECT * FROM submissions ORDER BY str_to_date(datecreated,'%d-%m-%Y %H:%i:%s') DESC;
+----+---------------------+
| id | datecreated         |
+----+---------------------+
|  3 | 16-11-2013 10:36:23 |
| 10 | 23-03-2013 23:04:52 |
|  4 | 02-08-2012 08:02:59 |
|  1 | 13-06-2012 21:52:34 |
|  7 | 02-10-2011 05:59:49 |
|  9 | 22-07-2011 14:04:19 |
|  2 | 04-07-2011 03:09:08 |
|  8 | 03-06-2011 03:55:04 |
|  6 | 06-01-2011 20:50:50 |
|  5 | 05-01-2011 20:54:16 |
+----+---------------------+
10 rows in set (0.00 sec)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top