|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Sorting Dates from current date
I need help sorting dates from the current date. The only way I have figured out how to do this, is with two queries.
The first query returns everything from the current day until the end of the year. Second query returns everything from the beginning of the year until the current date. So that the returned record set looks like this when running the query on 5/24. John - 5/25 Mary - 12/3 Joe - 1/6 I am using MS-Sql server, and have this in a stored procedure. When I exec the SP from an ASP page, it only returns the first recordset. SELECT Name, DatePart(M, Birthdate) As Month, DatePart(D, Birthdate) As Day FROM Users WHERE (DatePart(M, Birthdate) = DatePart(M, GetDate()) AND DatePart(D, Birthdate) >= DatePart(D, GetDate())) OR DatePart(M, Birthdate) > DatePart(M, GetDate()) ORDER BY DatePart(M, Birthdate) ASC, DatePart(D, Birthdate) ASC SELECT Name, DatePart(M, Birthdate) As Month, DatePart(D, Birthdate) As Day FROM Users WHERE (DatePart(M, Birthdate) = DatePart(M, GetDate()) AND DatePart(D, Birthdate) <= DatePart(D, GetDate())) OR DatePart(M, Birthdate) < DatePart(M, GetDate()) ORDER BY DatePart(M, Birthdate) ASC, DatePart(D, Birthdate) ASC |
|
#2
|
||||
|
||||
|
any chance you could edit the stored proc? what you want does not require two queries
rudy |
|
#3
|
|||
|
|||
|
Yes, I can edit the procedure, I just don't know how to write the query to accomplish the output needed.
|
|
#4
|
||||
|
||||
|
how do you want the dates sorted?
|
|
#5
|
|||
|
|||
|
They should sort by the birthmonth / birthday beginning with the current date, forward to the end of the year, then all dates beginning in January to the present date should show.
|
|
#6
|
||||
|
||||
|
Code:
select case
when month(Birthdate) < month(Getdate())
then 'past'
when month(Birthdate) = month(Getdate())
and day(Birthdate) < month(Getdate())
then 'past'
else 'future'
end as sortseq
, Name
, month(Birthdate) as birthmonth
, day(Birthdate) as birthmonthday
from Users
order
by sortseq
, month(Birthdate)
, day(Birthdate)
|
|
#7
|
|||
|
|||
|
That does the trick. Much thanks for your help
|
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > Sorting Dates from current date |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|