cfquery for date not working

Ask a Question related to Macromedia ColdFusion, Design and Development.

  1. #1

    Default cfquery for date not working

    Greetings - I am sure what I am missing is something simple. I am trying to
    query for a specific date in a database. However, when I search for a specific
    date, all I get is a blank page, no error message at all. If i simply output
    the entire db records, they all display just fine. Perhaps someone can shed
    some light on what really should be a simple straight forward process. DB:
    Access 2003 CF: CF5 & CFMX DB Field: StartDate w/ Data Type as Date/Time
    (no additional attributes) DB Field: RecordTitle w/ Data Type as Text ====
    Query ==== <cfquery name='GetDate' datasource='site_testing_db01'> SELECT
    RecordID, StartDate, RecordTitle FROM DateSearch WHERE StartDate = 4/1/2005
    <Displays blank page> </cfquery> ==== Output ==== <table width='70%'
    border='1' cellpadding='0' cellspacing='0'> <tr> <td
    align='center'>Date</td> <td align='center'>Record Title</td> </tr>
    <cfoutput query='GetDate'> <tr> <td width='20%'
    align='center'>#DateFormat(StartDate, 'mm/dd/yy')#</td> <td
    width='80%'>&amp;nbsp;&amp;nbsp;&amp;nbsp;#RecordT itle#</td> </tr> </cfoutput>
    </table> Thanks in advance for assistance Leonard B

    Leonard B Guest

  2. Similar Questions and Discussions

    1. CFQUERY not working for some reason
      I have the following code to authenticate a password, and it is successful, complete the <CFIF> Statement: <cfquery name="checkpass"...
    2. Always Applying Date Mask on cfquery (MySQL)
      ColdFusion is displaying my dates in an incorrect timestamp format. I understand that the date can be masked using the DateFormat() function, but...
    3. PreserveSingleQuotes Not Working in CFQUERY
      Just a heads up to those for those using CFSCRIPT within a CFQUERY to compose a Dynamic SQL statement. When you try to use...
    4. Working w/ Date
      :( Hello all, I'm trying to set up a query that gets all the information on tasks that have a due date of today. For some reason the query...
    5. date command not working in crontab
      I have this crontab entered: /usr/local/mysql/bin/mysqldump -u <username> -p<password> --all fg_phpnuke > /usr/local/mysql/var/db-fg_phpnuke`date...
  3. #2

    Default Re: cfquery for date not working

    The problem is that the field you're checking, StartDate, is a Date/Time and
    you're just passing it a Date. What you need to do is pull just the date out of
    the date/time object in the database.

    WHERE to_char(StartDate,'MM/DD/YYYY') = '05/15/2005'

    Kronin555 Guest

  4. #3

    Default Re: cfquery for date not working

    Not quite true,

    The where clause is actually saying

    where startdate = 4 divided by 1 divided by 2005

    Plus there is no need to convert the date on the db side, just pass it an odbc
    date data type

    SELECT RecordID, StartDate, RecordTitle
    FROM DateSearch
    WHERE StartDate = #CreateODBCDate("4/1/2005")#

    Ken

    The ScareCrow Guest

  5. #4

    Default Re: cfquery for date not working

    Ken,

    You're right on the 4/1/2005, I missed that.

    However, your code will do exactly what I said in my original post, which is
    compare a field in the database which contains a date and time with a date-only
    field (which assumes a time of midnight).

    So, if StartDate is 4/1/2005 12:15:03 and you do a search with a date of
    4/1/2005, it'll never match because you're comparing 4/1/2005 00:00:00 with
    4/1/2005 12:15:03. It is for this reason that I had the
    to_char(StartDate,'MM/DD/YYYY') in the query, to pull just the date part out
    and ignore the time.

    Kronin555 Guest

  6. #5

    Default Re: cfquery for date not working

    Kronin555

    I totally agree that if the field contains time values, then my code would not
    return a match. But if only the date was entered into the field then it will
    always have a time value of "00:00:00". Which I would recommend unless a time
    value is absolutly required. If the field does have a time value then other
    methods need to be used.

    As for your solution, I would be very careful with this method, as the the
    date passed hs to be in the exact format as the to_char format. But again I
    would not use the to_char function. I would use the DATEVALUE(date_text)
    function.

    But as I was taught that dates should always stay as dates, I never convert
    them when quering only for display. It just creates extra processing and
    possible bugs.

    Ken

    The ScareCrow Guest

  7. #6

    Default Re: cfquery for date not working

    There's a couple of reasons not to use DB date to string conversions. First, the
    DB has to convert every date in each row to a string to compare it to the
    value
    passed. Its much more efficient if it can compare the date value passed, to
    the
    stored dates directly. The other reason is that using DB functions makes the
    code less portable since the functions are usually specific to that DB.

    OldCFer Guest

  8. #7

    Default Re: cfquery for date not working

    Greetings - Found a working solution to situation in the Macromedia ColdFusion
    5 - Web Application Construction Kit Book - Ben Forta and Nate Weiss. WHERE
    StartDate >= #CreateODBCDate(form.DateSearch)# AND StartDate <
    #CreateODBCDate(DateAdd('d',form.DateSearch, 1))# Thanks for the input, took
    note of what you guys provided, and added to my resource records. Leonard

    Leonard B 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