• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Order by date question

I was wondering if there is a way to sort my query by date.  But instead of having it sort the normal way (either ASC or DESC), is there a way to have it sort by the item that is closest to today's date and then move on.

So for instance.  Say I have 10 items in my database ranging from Jan 04 to Dec 04.  Is it possible to list the item closes to August 8th first and the forward and possibly moving the earlier items to the bottom of the page.

If anything.  How can I display the item that is closest to todays date and future items.

Thank you,
Jeff
0
jeffmace
Asked:
jeffmace
  • 4
  • 3
  • 3
2 Solutions
 
eclipse2kCommented:
Hello Jeff!!

This is something, your SQL Server should manage. If you are using Microsoft SQL Server, you can do it that way:

-----------------
select      *
from      yourtable
order      by Abs(DateDiff(s, YOUR_DATE, DATE_COLUMN)) ASC
-----------------

for example

-----------------
select      *
from      yourtable
order      by Abs(DateDiff(s, '2004-25-05', [Timestamp])) ASC
-----------------

There is still a little problem, though: This selection is quite accurate, because the difference is measured in seconds (see the S in DateDiff function). But when you exceed the timespan of 68 Years, it will cause an error, an you will need to use MI for minutes :)


Eclipse2k

0
 
eclipse2kCommented:
Its me again,
here is a real example i tested:

-----------------
select      TOP 25 [Timestamp]
from      logging
order      by Abs(DateDiff(s, '2004-27-07 12:30:00', [Timestamp])) ASC
-----------------

Results:

-----------------
2004-07-27 12:34:52.760
2004-07-27 12:34:53.167
2004-07-27 12:24:28.780
2004-07-27 12:24:28.860
2004-07-27 12:37:50.183
2004-07-27 12:37:50.230
2004-07-27 12:37:50.230
2004-07-27 12:45:37.847
2004-07-27 12:45:37.893
2004-07-27 12:45:37.910
2004-07-27 12:13:35.337
2004-07-27 12:13:35.400
2004-07-27 12:13:32.260
2004-07-27 12:13:32.320
2004-07-27 12:13:31.040
2004-07-27 12:13:30.993
2004-07-27 12:13:29.010
2004-07-27 12:13:28.947
2004-07-27 13:13:08.030
2004-07-27 13:13:08.093
2004-07-27 13:14:31.743
2004-07-27 13:14:31.773
2004-07-27 13:15:27.257
2004-07-27 13:15:27.287
2004-07-27 13:15:27.303
-----------------

Eclipse2k
0
 
jeffmaceAuthor Commented:
Here is my query:  I am not sure how this works with this [TimeStamp] function.  I am using SQL 2000, but I am not a pro at SQL.

<CFQUERY name="getMeetings" datasource="#ds#">
SELECT     CMEProgramSym.*, CMEPrograms.CMEProgramName, CMEPrograms.CMEProgramSymposiumName,
                      Users.*
FROM         CMEProgramSym INNER JOIN
                      CMEPrograms ON CMEProgramSym.CMEProgramID = CMEPrograms.CMEProgramID INNER JOIN
                      Users ON CMEPrograms.CMEProgramSalesRep = Users.UserID
WHERE CMEProgramSym.CMEProgramID = '#URL.ID#'
ORDER by Abs(DateDiff(s, CMEProgramSym.CMEProgramSymDate, [TimeStamp])) ASC
</CFQUERY>


The CMEProgramSymDate field is the date field that will help sort the order.
0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
mrichmonCommented:
put NOW() in where you have [Timestamp]
0
 
jeffmaceAuthor Commented:
Well I got the query to work, but the order is very messed up now even though its correct by the datediff function.

08/11/04
08/3/04
08/18/04
07/21/04
07/14/04
09/09/04
07/08/04
09/22/04

I was hoping for it to start here at the 8/11 and move on forward. so at least I can have the most current meeting at the top.
0
 
eclipse2kCommented:
@mrichmon

i guess that diffing with Now() would not return the expected result, because the task is to check from Any given point of time. Your solution would be finally the same like ordering by CMEProgramSym.CMEProgramSymDate (in the given case)

@jeff

i really wonder about the results you get...
Please check that...

 - CMEProgramSymDate is a DateTime SQL Variable Type..?
 - Where [TimeStamp] stands, you in fact need the date to be checked, like... '2004-08-08 12:00:00' or anything like that (i dont know your locale Date Format since i use the german)

Possible example, not tested:

-------------------
<!--- CreateDateTime Arguments: Year, Month, Day, Hour, Minute, Second --->
<cfset checkDate = CreateDateTime(2004,8,8,12,0,0)>

<CFQUERY name="getMeetings" datasource="#ds#">
SELECT     CMEProgramSym.*, CMEPrograms.CMEProgramName, CMEPrograms.CMEProgramSymposiumName,
                      Users.*
FROM         CMEProgramSym INNER JOIN
                      CMEPrograms ON CMEProgramSym.CMEProgramID = CMEPrograms.CMEProgramID INNER JOIN
                      Users ON CMEPrograms.CMEProgramSalesRep = Users.UserID
WHERE CMEProgramSym.CMEProgramID = '#URL.ID#'
ORDER by Abs(DateDiff(s, CMEProgramSym.CMEProgramSymDate, <cfqueryparam cfsqltype="cf_sql_date" value="#checkDate#">)) ASC
</CFQUERY>
-------------------

eclipse2k
0
 
mrichmonCommented:
Not really.  jeffmace mentioned "How can I display the item that is closest to todays date" so in that case Now() would work.
0
 
jeffmaceAuthor Commented:
CMEProgramSymDate is smalldatetime in SQL and it looks like this in the database '5/24/2003'  

Maybe that will help clear up the issue, I am not sure if there is a different way to code for that setup.

Jeff
0
 
eclipse2kCommented:
@Jeff

It looks like you dont have a Date Format without time?
You could try to use "mi" instead of "s" for the DateDiff Function.

Also you could check out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_da-db_5vxi.asp

Quote from site: "...Because smalldatetime is accurate only to the minute, when a smalldatetime value is used, seconds and milliseconds are always 0."


@mriichmon

Sorry, i got it wrong because i read "list the item closes to August 8th..." and i really didnt notice that this was the post date :) I'm sorry, you were right
0
 
mrichmonCommented:
it's ok :o)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now