Skip to main content
Inspiring
February 18, 2010
Question

My query needs work

  • February 18, 2010
  • 3 replies
  • 334 views

I have imported an excel spreadsheet into access and I'm going against this access db with my cfml.

One of my columns is titled CompletionDate and it has a date in it or it is empty. The trouble is that I'm getting an incorrect count because I am saying to count it if it is not null. Therefore I think it is picking up spaces and things like that in addition to records that have dates in them. Is there a better way to say give me just records that have a date in it? thank you.

<!--- Total number of completions in company. --->
<cfquery name="CompletionsCount" datasource="#application.DSN#" dbtype="ODBC">
SELECT    CompletionDate
FROM   RawData
WHERE    Status = 'Active'
<cfinclude template="dsp_ListOfExcludedEmps.cfm">
And    CompletionDate is not null
</CFQUERY>

    This topic has been closed for replies.

    3 replies

    Participating Frequently
    February 19, 2010

    Winston2, first, I would advocate always trimming your field values on their way in, or somehow excluding this blanks and spaces in your date fields.  It's always beneficial to make sure your database's integrity is in check, rather than bandaid a problem, because those will typically come back and bite you in the butt later (for example, when you want to perform some kind of date function on these date fields that are allowing spaces in them).

    Having said that, here's what you can do if that's not an option:

    <cfquery name="CompletionsCount" datasource="#application.DSN#" dbtype="ODBC">
         SELECT    CompletionDate
         FROM   RawData
         WHERE    Status = 'Active'
         <cfinclude template="dsp_ListOfExcludedEmps.cfm">
         AND    CompletionDate is not null

         AND Len( Trim( CompletionDate ) ) > 0
    </cfquery>

    Basically, with that last "AND" clause, you're trimming your CompletionDate fields of extra spaces, and then comparing the resulting length of the field.  If it thereafter doesn't have any characters in it, you'll exclude that row / record from your query.  If after the trim function, the field still has characters (i.e, dates), then you're including it in your query.

    Again, though, this is a bandaid, and I would recommend correcting the data if you are able to! 

    Inspiring
    February 18, 2010

    I have imported an excel spreadsheet into access ...

    it has a date in it or it is empty.

    I do not know what import process you used, or the data type of the "CompletionDate" column. But before you can figure out what type of comparison to use, you need to look at the data. How did your import process handled those "empty" values? ie What value were they converted into?

    Inspiring
    February 18, 2010

    What is the final SQL statement that is being passed to the DB which is - I'm guessing - erroring?

    And what's the actual error?

    --

    Adam