Date format error in ColdFusion and mySQL

回眸只為那壹抹淺笑 提交于 2019-12-12 17:34:00

问题


I have set up a file that reads an xml file from one database and inserts that data into another. All works well except a strange error on the date format. The date format I need is yyyy-mm-dd. However, the original data is set up as format dd-mm-yyyy.

My code reads and inserts all the data, but when it reads the date field, there is an issue when the day is under 12. It strangely inserts it the other way around.

  • If a date is 11/10/2014, it pulls it in as 2014/11/10
  • But, if a date is 13/10/2014, it pulls it in as 2014/10/13 (which is correct and what I need).

If I set the MySQL field type to text or varchar, it inserts the value in the correct order, but of course not in a date format for ColdFusion. So it inserts a date as 11/10/2014, but when I set the same field to type 'date' or even 'date time' format, it inserts it in the correct format '2014-10-11 but with the issue above.

The code I'm currently working with is:

<cfif isDate(arrA[currentField])>
   #currentField# = '#LSdateformat(arrA[currentField],"yyyy-mm-dd")#'
<cfelse>
   #currentField# = '#arrA[currentField]#'
</cfif>

My second attempt was using the following:

<cfif isDate(arrA[currentField])>
   #currentField# = '#ParseDateTime(arrA[currentField],"yyyy-mm-dd")#'
<cfelse>
...

I then receive the following error:

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 '1900-01-01 00:00:00'}' , ScheduleReferenceNumber = 'TS8279631' ,' at line 15

On the third attempt, I used:

<cfloop collection="#arrA#" item="currentField">
    <cfif currentField NEQ 'TypeDesc'>
        <cfif fieldcount NEQ 0>,</cfif>
        <cfif currentField eq 'startDate'>
            <cfqueryparam cfsqltype="cf_sql_date" value="#currentField#">
        <cfelse>
            #currentField# = '#arrA[currentField]#'
        </cfif>
        <cfset fieldCount=fieldCount+1>
    </cfif>
 </cfloop>

But I then get the error:

The cause of this output exception was that: coldfusion.runtime.locale.CFLocaleBase$InvalidDateTimeException: StartDate is an invalid date or time string..

Having also tried:

<cfset DateLocale = "English (UK)">
<cfset DateString = "11/10/2014">
<cfloop collection="#arrA#" item="currentField">
    <cfif currentField NEQ 'TypeDesc'>
        <cfif fieldcount NEQ 0>,</cfif>
            <cfif currentField eq 'startDate'>
                <cfqueryparam value="#LSParseDateTime(dateString, dateLocale)#" cfsqltype="cf_sql_timestamp">
            <cfelse>
                #currentField# = '#arrA[currentField]#'
            </cfif>
            <cfset fieldCount=fieldCount+1>
         </cfif>
</cfloop>
<cfif checkRecord.recordcount neq 0>
    WHERE  ScheduleReferenceNumber = '#arrA.ScheduleReferenceNumber#'
</cfif> 
</cfquery>

I get the following:

Error Executing Database Query.

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 ''2014-10-11 00:00:00.0' , Currency = '' , Tutor = '' , CourseLoc' at line 17


回答1:


You forgot to specify a Locale. When omitted, the LS date functions use the current page's Locale. Sounds like yours is "English (US)". Per U.S. date conventions, month is always first. So the string "11/10/2014" will always be treated as November 10th, not October 11th.

That said, if you are populating a date/time column, you should use cfqueryparam and pass date objects to the database, rather than date strings, which can be misinterpreted, depending on database settings.

One option is to use LSParseDateTime() with an appropriate Locale. Do not use ParseDateTime(). Like most of the standard date functions, it always uses U.S. date rules, and will produce the same wrong results you are getting now.

   <cfset DateLocale = "English (UK)">
   <cfset DateString = "11/10/2014">
   ...
   <cfquery ...>
       INSERT INTO Table ( SomeColumn )
       VALUES (
            <cfqueryparam value="#LSParseDateTime(dateString, dateLocale)#"
                    cfsqltype="cf_sql_timestamp">
       )
   </cfquery>

Also, for validation, be sure to use the LS version of IsDate. Again with the appropriate Locale.

<cfif LSIsDate( dateString, dateLocale )>
     ....
</cfif>

NB: Keep in mind CF's date functions are notoriously "generous" about what is considered valid. If the format of your date strings can vary, you may want to implement your own date validation.




回答2:


Assuming your database datatype is date or something similar, you are using an inappropriate function. LSdateformat() converts a date to a string. The function that converts a string to a date is ParseDateTime().

If your date fields are always arriving with the same format, you can hard code the appropriate mask argument of function ParseDateTime(). If they vary, you'll have to use conditional logic to determine the correct mask.

You should also be cautious about using isDate(). It returns true on some unexpected values such as "apr 31". Combining it with ReFind() and len() would be more thorough.



来源:https://stackoverflow.com/questions/24170245/date-format-error-in-coldfusion-and-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!