Ask a Question related to Coldfusion Database Access, Design and Development.
-
Explorer5 #1
Finding a date closest to a specific date
I have a table which has a bunch of dates, from 2003, all the way until 2006.
I'm trying to figure out an SQL statement which will allow me to find a date in
the table which is closest to a specific date.
For example, i want to find a date which is closest to todays date, whether
the date is in the past or the future doesnt matter, as long as its the closest
to today...
Anyone have any ideas?
Explorer5 Guest
-
How query specific date parts
If my database have a date field (eg: dir_date) with the following dates: 23/06/2005, 24/06/2005, 27/06/2005, 03/07/2005, 08/07/2005, 14/07/2005,... -
DateDiff days before a specific date
I need to determine the date of an event based off of a date in the future. Example: Future Date: 03/10/2005 Date Needed: 10 days before 03/10/2005... -
Querying a date closest to today
How can i write a query that looks for the date that is closest to today, and only returns one record? I dont want to do a date range because the... -
File sorting by a specific date
I want to search a directory of log files and populate a list of those log files ONLY if they match today's date (localtime). $logs =... -
How do I manipulate a date variable to a specific date array?
Hi, I use the getdate() function to return today's date in an array. I do this as I need to separate the day/month/year as to display them in a... -
The ScareCrow #2
Re: Finding a date closest to a specific date
maybe something like:
Select Min(myColumn) As theDate
From myTable
Where myColumn > #CreateODBCDate(Now())#
Or myColumn < #CreateODBCDate(Now())#
Ken
The ScareCrow Guest
-
cjeris #3
Re: Finding a date closest to a specific date
At least on MS SQL Server, you can do it like this:
SELECT TOP 1 [id], [date]
FROM [my_table]
ORDER BY ABS(DATEDIFF(s, GETDATE(), [date])) ASC
DATEDIFF(s, GETDATE(), [date]) is the signed difference in seconds between the
[date] column and the current server date-time GETDATE(). ABS(...) computes
the absolute value because you don't care whether [date] is before or after
now. Then you pick the single first (smallest difference) row.
I do not know whether this query calls GETDATE() once for every row or every
comparison. If it does, you should probably move the call out of the ORDER BY
clause:
DECLARE @the_now DATETIME;
SET @the_now = GETDATE();
SELECT TOP 1 [id], [date]
FROM [my_table]
ORDER BY ABS(DATEDIFF(s, @the_now, [date])) ASC
cjeris Guest



Reply With Quote

