views:

134

answers:

5

I would like to pass a MYSQL query via Coldfusion the following date: 03/13/2010 So the query filters against it like so:

SELECT *
FROM myTable
WHERE dateAdded before or on 03/13/2010

I'd also like to be able to take 2 dates as ranges, from: 01/11/2000, to: 03/13/2010

SELECT *
FROMT myTable
WHERE dateAdded is ON or Between 01/11/2000 through 03/13/2010

Also, Is there a way to have one query that can handle either BOTH Date_Start and Date_END, or just one of the two?

thanks

+1  A: 
SELECT *
FROM myTable 
WHERE dateAdded <= YourdateValue

or

SELECT *
FROM myTable 
WHERE dateAdded BETWEEN YourdateValueStart AND YourdateValueEnd

Have a look at STR_TO_DATE(str,format)

This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL and produces a warning. The server scans str attempting to match format to it. The format string can contain literal characters and format specifiers beginning with %. Literal characters in format must match literally in str. Format specifiers in format must match a date or time part in str. For the specifiers that can be used in format, see the DATE_FORMAT() function description.

SELECT * 
FROM myTable  
WHERE (dateAdded <= YourdateValueStart AND YourdateValueEnd  IS NULL)
OR  (dateAdded >= YourdateValueEnd AND YourdateValueStart  IS NULL)
OR (dateAdded BETWEEN YourdateValueStart AND YourdateValueEnd)
astander
How do I format the string "03/12/2010" to be acceptable for what you have above: YourdateValue
AnApprentice
+3  A: 

If you cannot format your date to YYYY-MM-DD in Coldfusion, you can use the STR_TO_DATE function in MySQL as follows:

SELECT 
    *
FROM 
    myTable 
WHERE 
    dateAdded <= STR_TO_DATE('03/13/2010','%m/%d/%Y');

And...

SELECT 
    *
FROM 
    myTable 
WHERE 
    dateAdded BETWEEN STR_TO_DATE('01/11/2000','%m/%d/%Y') AND 
                      STR_TO_DATE('03/13/2010','%m/%d/%Y');
Daniel Vassallo
+1: Beat me to it.
OMG Ponies
Is there a way to have one query that can handle either BOTH Date_Start and Date_END, or just one of the two?
AnApprentice
@nobosh: Would it be possible to show us your coldfusion code? We may be able to suggest a better solution.
Daniel Vassallo
This doesn't work?
AnApprentice
@nobosh: Sorry, I just noticed that your date is in MM/DD format not DD/MM. Fixed my answer.
Daniel Vassallo
dateAdded is a timestamp in the DB, so I added a DATE_FORMAT(dateAdded) which seemed to help... The error is now: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') BETWEEN STR_TO_DATE('03/01/2010','%d/%m/%Y') AND STR_TO_DATE('03/24/2010','' at line 22
AnApprentice
AnApprentice
Another issue here that I'm seeing is that it says BETWEEN,, if the date_to is 3/13/2010, right now that's not coming back since it's equal to.. Is there a way to update the above to work for equal to?
AnApprentice
Yes `Y` in caps means year in 4 digits. `M` in caps would mean a long month name, such as January, February, etc. Full details here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
Daniel Vassallo
@Daniel Vassallo, thanks I didn't know that... I'm still confused why it's not returning records for TODAY.. Here is the line, note the Date Added is a timestamp, could that be the issue? AND (myTable.dateAdded) BETWEEN STR_TO_DATE('#attributes.datepicker_from#','%m/%d/%Y') AND STR_TO_DATE('#attributes.datepicker_to#','%m/%d/%Y')
AnApprentice
@nobosh: The `BETWEEN` operator is equivalent to `>= AND <=`, so it should include the start date and the end date. Source: http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#operator_between
Daniel Vassallo
@nobosh: I guess the reason that your query is not returning results for today is because the dateAdded field is a DateTime field, and includes the time (not just the date). Therefore `03/13/2010 11:00:00` would be bigger than `03/13/2010`. To fix that, you can include 23:59:59 to the end date like this: `AND STR_TO_DATE('#attributes.datepicker_to# 23:59:59','%m/%d/%Y %H:%i:%s')`
Daniel Vassallo
That makes sense. So I need to convert the dateTime field to a date field?
AnApprentice
That did it, thank you!!!!
AnApprentice
Good news! I'm glad it worked.
Daniel Vassallo
Of course you can format the date in ColdFusion: `dateFormat(myDate,"yyyy-mm-dd")`. Shouldn't be strictly necessary, though; if you're using CFQUERYPARAM the format of the date should be adjusted for you based on the database driver being used.
Al Everett
A: 

I believe in MySQL you can use comparison operators on dates

so I think you can

SELECT *
FROM myTable
WHERE dateAdded  <= 20100111

and

SELECT *
FROMT myTable
WHERE dateAdded is  BETWEEN 20000111 AND 20100303

In general, here's a list of date manipulation functions that mySQL offers: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.htm

Anatoly Fayngelerin
+2  A: 

The best way to do it in Coldfusion would be to use cfqueryparam to manage the translation of the string to a date object as well as providing a level of SQL injection prevention.

<cfset myResult = queryNew("id")>
<cfset startDate = "01/11/2000">
<cfset endDate = dateFormat(now(), "mm/dd/yyyy")>

<cfquery name="myResult" datasource="myDatasource">
    SELECT *
    FROM myTable 
    WHERE dateAdded BETWEEN <cfqueryparam CFSQLType="CF_SQL_DATE" value="#startDate#">
    AND <cfqueryparam CFSQLType="CF_SQL_DATE" value="#endDate#">
</cfquery>

<cfoutput query="myResult">
 ...output code here
</cfoutput>
Dan Sorensen
A: 

You can use cfqueryparam with a cfsqltype of CF_SQL_TIMESTAMP. Given both a "startDate" and "endDate" variable the below code should work. If either startDate or endDate is set to something like false then it won't be included:

<cfset startDate = createDate(2010, 01, 19) />
<cfset endDate  = createDate(2010, 01, 26) />

<cfquery name="q" datasource="#request.dsn#">
    SELECT *
    FROM myTable
    WHERE 
    (
        <cfif isDate(startDate)>
            dateAdded >= <cfqueryparam value="#dateFormat(startDate, "YYYY/MM/DD")#" cfsqltype="CF_SQL_TIMESTAMP" />
        </cfif>
        <cfif isDate(endDate)>
            <cfif isDate(startDate)>AND</cfif>
            dateAdded <= <cfqueryparam value="#dateFormat(endDate, "YYYY/MM/DD")#" cfsqltype="CF_SQL_TIMESTAMP" />
        </cfif>
    )
</cfquery>

Try running the same thing, but with either startDate or endDate set to false such as below to see how it all works in a single query:

<cfset startDate = false />
<cfquery name="q" datasource="#request.dsn#">....
Greg S