Jump to content

Sorting a date hired field for anniversaries


Mark1inLA

Recommended Posts

Hi all,

 

I've been trying to figure this out for quite sometime and haven't been able to resolve it yet. Here is some sample data:

 

id

name

hire_date

1

john

2005-01-10

2

jane

2009-12-10

3

bill

2007-11-10

 

How can i get it to sort from the most recent anniversary date in descending order so that the order will show in this order: John, Jane, Bill

 

Thanks in advance!

 

Link to comment
Share on other sites

SELECT ... FROM `yourtable` WHERE ... ORDER BY `hire_date` DESC

 

in this order: John, Jane, Bill

That's not descending order...

 

I'm not exactly sure about descending or ascending orders for dates, so let me restate: from the most recent anniversary dates to the later.

Link to comment
Share on other sites

When you say descending / ascending order, are you not concerned with the year portion?

 

I think what you will want to do is:

create table `myjunktable` (
name varchar( 22 ),
hire_date datetime
);

INSERT INTO myjunktable( name, hire_date ) 
VALUES (
'john',  '2005-01-10'
), (
'jane',  '2009-12-10'
), (
'bill',  '2007-11-10'
);

select 
*
from myjunktable
order by 
    -- We want to order by the number of days between today and the anniv_date
    -- regardless of sign, so we wrap it all in abs()
    abs( 
        -- The datediff calculates the number of days between the current time
        -- and that returned by the concat.
        -- If anniv_date is 2010-01-05 and today is 2010-01-13, datediff is: 8
        -- If anniv_date is 2010-01-14 and today is 2010-01-13, datediff is: -1
        datediff( 
            now(), 
                -- The concat takes the hire_date and calculates the anniversary 
                -- for this year.  i.e. hire_date,   anniv_date
                --                   2005-01-05         2010-01-05
                --                   2007-06-08         2010-06-08
                concat(             
                    date_format( now(), '%Y' ),          -- Returns current year as YYYY, i.e. 2010
                    date_format( hire_date, '-%m-%d' )   -- Returns month-day portion of hire_date, 
                                                         -- i.e. if hire_date is 2005-01-05, returns -01-05
                ) 
            ) 
        );

Link to comment
Share on other sites

When you say descending / ascending order, are you not concerned with the year portion?

 

I think what you will want to do is:

create table `myjunktable` (
name varchar( 22 ),
hire_date datetime
);

INSERT INTO myjunktable( name, hire_date ) 
VALUES (
'john',  '2005-01-10'
), (
'jane',  '2009-12-10'
), (
'bill',  '2007-11-10'
);

select 
*
from myjunktable
order by 
    -- We want to order by the number of days between today and the anniv_date
    -- regardless of sign, so we wrap it all in abs()
    abs( 
        -- The datediff calculates the number of days between the current time
        -- and that returned by the concat.
        -- If anniv_date is 2010-01-05 and today is 2010-01-13, datediff is: 8
        -- If anniv_date is 2010-01-14 and today is 2010-01-13, datediff is: -1
        datediff( 
            now(), 
                -- The concat takes the hire_date and calculates the anniversary 
                -- for this year.  i.e. hire_date,   anniv_date
                --                   2005-01-05         2010-01-05
                --                   2007-06-08         2010-06-08
                concat(             
                    date_format( now(), '%Y' ),          -- Returns current year as YYYY, i.e. 2010
                    date_format( hire_date, '-%m-%d' )   -- Returns month-day portion of hire_date, 
                                                         -- i.e. if hire_date is 2005-01-05, returns -01-05
                ) 
            ) 
        );

 

 

Problem solved!

 

Thanks a bunch for your help and detailed comments.

 

Link to comment
Share on other sites

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.