Skip to main content
Participant
October 27, 2009
Question

Getting this on a query.

  • October 27, 2009
  • 1 reply
  • 812 views

Getting this on a query.

Parameters: nextthursday = {ts '2009-10-29 00:00:00'} lastthursday = 2009-10-22 00:00:00.000

<cffunction access="public" name="qry_contestthisweek_get" output="false"  returntype="query"> 

<cfargument name="lastthursday" type="string" default="">  

  <cfset nextthursday = dateadd("d", 7, arguments.lastthursday)>

  <cfquery       name="qry_contestthisweek_get"       datasource="#variables.datasource#">    

  select    *       from     #variables.sqlobjectprefix#contest

      where    1 = 1     

       and addeddate between <cfqueryparam  value="#createodbcdatetime(arguments.lastthursday)#"  cfsqltype="CF_SQL_TIMESTAMP"> and <cfqueryparam  value="#createodbcdatetime(nextthursday)#" cfsqltype="CF_SQL_TIMESTAMP">  

          order by score, age     </cfquery>

  <cfreturn qry_contestthisweek_get>

</cffunction>

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    October 27, 2009

    You shouldn't use the variables scope in a function. Ideally, the variables in a function should be local to the function.

    Pass variables as arguments instead. That way, any one can call the function, which can only be a good thing.

    The functions createodbcdatetime and dateadd accept a date, not a string, as parameter. Hence the need to use parsedatetime to convert first from string to date object.

    <cffunction access="public" name="qry_contestthisweek_get" output="false"  returntype="query">
    <cfargument name="lastthursday" type="string" required="yes">
    <cfargument name="datasource" type="string" required="yes">
    <cfargument name="tablename" type="string" required="yes">

    <!--- query name made local to this function --->

    <cfset var qry_contestthisweek="">
    <!--- convert string to date --->
    <cfset lastThursdayDate = parseDatetime(arguments.lastthursday)>
    <cfset nextThursdayDate = dateadd("d", 7, lastThursdayDate)>
    <cfquery  name="qry_contestthisweek" datasource="#arguments.datasource#">   
        select    *      
        from     #arguments.tablename#
        where    1 = 1    
        and addeddate between <cfqueryparam  value="#createodbcdatetime(lastThursdayDate)#"  cfsqltype="CF_SQL_TIMESTAMP"> and <cfqueryparam  value="#createodbcdatetime(nextThursdayDate)#" cfsqltype="CF_SQL_TIMESTAMP"> 
        order by score, age    
    </cfquery>
    <cfreturn qry_contestthisweek>
    </cffunction>

    Participant
    October 27, 2009

    Still get this:

    [Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC SQL Server  Driver]Invalid precision value

    <cffunction access="public" name="qry_contestthisweek_get" output="false"  returntype="query">

    <cfargument name="lastthursday" type="string" required="yes">

    <!--- convert string to date --->

    <cfset lastThursdayDate = parseDatetime(arguments.lastthursday)>

    <cfset nextThursdayDate = dateadd("d", 7, lastThursdayDate)>

    <cfquery  name="qry_contestthisweek_get" datasource="#variables.datasource#">  

        select    *     

        from     #variables.sqlobjectprefix#contest

        where    1 = 1   

        and addeddate between <cfqueryparam  value="#createodbcdatetime(lastThursdayDate)#"  cfsqltype="CF_SQL_TIMESTAMP"> and <cfqueryparam  value="#createodbcdatetime(nextThursdayDate)#" cfsqltype="CF_SQL_TIMESTAMP">

        order by score, age   

    </cfquery>

    <cfreturn qry_contestthisweek_get>

    </cffunction>

    Inspiring
    October 27, 2009

    Make your argument type = date instead of type = string.  Don't have a default.   Also, in your cfqueryparam, use date instead of timestamp for your data type, and don't bother with any functions.