Finding a date closest to a specific date

Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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,...
    2. 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...
    3. 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...
    4. 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 =...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139