Skip to main content
Participant
April 1, 2016
Question

cfset no longer works in 2016

  • April 1, 2016
  • 2 replies
  • 1028 views

Hi,

Can anyone tell me please why this:

<cfquery name = "check" datasource="ds">SELECT Date FROM tbl</cfquery>

<cfset check.Date = DateFormat(check.Date, "short")>

results in the check.Date changing on versions before CF 2016 but not in CF 2016?

thanks.

    This topic has been closed for replies.

    2 replies

    Carl Von Stetten
    Legend
    April 1, 2016

    BadMrFrosty You shouldn't be able to update the value of a query "cell" that way.  It might help to think of a query result as an array of structs (it's actually more complicated under the hood, but in many ways it does behave this way).  Each row of the query is one item in the array, and the item is a struct containing key/value pairs matching your query columns and their values.  Even if your query only returns one row, it would still be an array (it would just have one struct item).  So to set the value of that cell, you would have to reference the location in the array (aka the row number).  So

    <cfset check.Date = DateFormat(check.Date, "short")>

    should be

    <cfset check[1].Date = DateFormat(check.Date, "short")>

    where check[1] references the first row of the query.  Unless, of course, the query has more than one row, in which case you have to specify the correct row (or more likely, loop over the query and do it to every row).


    However, as haxtbh‌ and WolfShade‌ have indicated, it would be better to fix the formatting in the SQL statement.

    BKBK
    Community Expert
    Community Expert
    April 2, 2016

    <cfset check.Date = DateFormat(check.Date, "short")>

    My thoughts are similar to Carl's. Coldfusion, being weakly typed, tolerates plenty. But you shouldn't be doing that in the first place.

    The left-hand side represents a column. Its type is coldfusion.sql.QueryColumn. The right-hand side is a string. Yet you are attempting to update the one with the other. Such code is bound to lead to inconsistency sooner or later. The fact that the code fails now is actually a blessing in disguise.

    I would just do

    <cfset checkDate = DateFormat(check.Date, "short")>

    Anit_Kumar
    Inspiring
    April 1, 2016

    It's working. Can you try the below example.

    <cfset abc= now()>

    <cfoutput>value of abc = #abc#</cfoutput>

    Regards,

    Anit Kumar

    Participant
    April 1, 2016

    Yes, <cfset abc = now()> does work however the example that I posted no longer works.

    Why can you no longer overwrite a variable?

    WolfShade
    Legend
    April 1, 2016

    Hi, BadMrFrosty,

    My first suggestion would be to modify the SQL so that it returns the date in the format you desire, instead of modifying the query object contents.

    I have not worked with CF2016, so I cannot say for sure.  Does the cfset generate an error in CF2016?  Is there anything in the CFAdmin logs that relates to your issue?

    HTH,

    ^_^

    (I am not, now, nor have I ever been an employee or consultant for Adobe Systems.

    The views, opinions, or code examples I present are mine - except some code examples

    may be retrieved from third-party sources - and cannot be construed as being expressly nor

    implicitly the views, opinions, or code examples of Adobe Systems,

    nor any affiliated, parent, or child entity of Adobe Systems.)