Jump to content

[SOLVED] custom sorting order of dates in MySQL


Recommended Posts

I have some data I wish to sort by date (ascending), however, some of the dates are missing and thus have the default '0000-00-00' as their entry.

 

When sorting in ascending order these appear at the top of the list, is there a way to put them at the bottom of the list but still have the rest in ascending order? (I know I can do two queries but I'd like one if possible.)

Ah, found a solution:

ORDER BY strcmp(date,'0000-00-00') DESC, date ASC

 

This works since strcmp(date,'0000-00-00') is 0 if the date is '0000-00-00' and 1 if a "higher string" (later alphabetically). So everything with a real date comes before that without from the first ordering, then the second ordering takes care of the entries with a date.

OK this was working on localhost but it's not working now I've uploaded to the internet, My host has MySQL client version 4.1.22. According to the MySQL manual, the STRCMP function existed in this version.

 

I just ran a query with

SELECT id, name, date, strcmp( date, '0000-00-00' ) ...

and this returns 1 for everything, whether a real date or '0000-00-00'.

 

Any idea why this might be?!

Are you sure your column is of type DATE and not of DATETIME?

 

Either of these ways would work (the date column in these is assumed a DATETIME type):

 

select STRCMP(DATE(date_column), '0000-00-00') AS sort_order, date_column from table_name

order by sort_order DESC, date_column ASC

;

 

select IF(DATE(date_column) = '0000-00-00', 0, 1) AS sort_order, date_column from table_name

order by sort_order DESC, date_column ASC

;

 

Yes, Barand's approach is a shortcut. It may not be as clear to a third person trying to look at the query and decipher it.

 

If it's a DATETIME column you'll still need this:

 

... order by date(date_column) = '0000-00-00' ASC, date_column ASC

 

or this:

 

... order by date_column = '0000-00-00 00:00:00' ASC, date_column ASC

 

 

Thanks, the simple = thing worked. The field is definitely a date field, still don't know why it doesn't work on the earlier version of MySQL but is works now, that's all that matters.

 

Final 'order by' clause:

ORDER BY date='0000-00-00' ASC, date ASC

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.