Jump to content

So we were tasked to sort birthday records by month and date, then year.

 

Here's my solution:

SELECT userbirthdayFROM usertableORDER BY DATEPART(DY, userbirthday) ASC, YEAR(userbirthday) ASC;

While you can just sort it by userbirthday, it is not what the requirements state.

We were given a tip that at the minimum, we should use ORDER BY twice.

 

What do you think of my solution guys ?

CPU: Intel i5-4590 | Motherboard: Asus H97M-E | GPU: Sapphire Nitro R9 390 | RAM: 2x4Gb Kingston HyperX Fury Black | SSD: Sandisk Plus 240Gb HDD: Seagate 250Gb  | PSU: Seasonic G650 80+ Gold | Case: NZXT S340

I am who I am.

Link to comment
https://linustechtips.com/topic/526085-date-sorting-using-ms-sql/
Share on other sites

Link to post
Share on other sites

I can't test this as I don't have a local SQL server to test on at home but you could try this?

 

orderby datepart(dd, bday), datepart(mm, bday), datepart(yy, bday) asc;

 

this will order by day then month then year.

 

you only need to specify the direction (asc, desc) once at the end of the statement.

Link to post
Share on other sites

orderby datepart(dd, bday), datepart(mm, bday), datepart(yy, bday) asc;

 

 This was the first solution I came up with. Then we were told that we should use order by twice.

CPU: Intel i5-4590 | Motherboard: Asus H97M-E | GPU: Sapphire Nitro R9 390 | RAM: 2x4Gb Kingston HyperX Fury Black | SSD: Sandisk Plus 240Gb HDD: Seagate 250Gb  | PSU: Seasonic G650 80+ Gold | Case: NZXT S340

I am who I am.

Link to post
Share on other sites

They tell you to order by 3 criteria and say you can only use orderby twice?  :huh:

 

There are only two criterias:

1. Month and Date

2. Year

 

Since I don't know of any functions that get's Month and Date at the same time, I decided to use DY Datepart.

CPU: Intel i5-4590 | Motherboard: Asus H97M-E | GPU: Sapphire Nitro R9 390 | RAM: 2x4Gb Kingston HyperX Fury Black | SSD: Sandisk Plus 240Gb HDD: Seagate 250Gb  | PSU: Seasonic G650 80+ Gold | Case: NZXT S340

I am who I am.

Link to post
Share on other sites

There are only two criterias:

1. Month and Date

2. Year

 

Since I don't know of any functions that get's Month and Date at the same time, I decided to use DY Datepart.

 

Month and Date doesn't make any sense, they should really clarify this. I think DY is a safe bet from what you're saying.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×