7

我正在使用 cfspreadsheet 从 Excel 工作表中读取值,然后我进行查询以对日期字段进行排序,因为 cfspreadsheet 返回的所有字段都是“VarChar”类型。这是代码:

<cfspreadsheet action = "read" query = "mySpreadsheet" src = "mp.xls" sheet="1" rows="2-178"> 

<cfquery name="mySpreadsheet2" dbtype="query">
 select  
(CAST(date_field as DATE))   as mydate

from mySpreadsheet order by mydate

</cfquery>

在电子表格中,日期采用欧元格式 dd/mm/yy。问题是查询查询中的 CAST 函数将 'date_field' 从 varchar 转换为日期,但美国类型的日期(第一个月份,后一天)。

例如,excel date_field 列包含此值 01/07/2011(2011 年 7 月一号,因为它是欧洲日期)​​,但它在查询查询中被转换为 {ts '2011-01-07 00:00:00'}。

有没有办法在查询查询中使用 CAST 来生成欧式日期?要在 oracle 中解决这个问题,你可以这样做: to_date(date_field, 'DD:MM:YY') 但我不知道如何在这里解决这个问题。

4

3 回答 3

3

As mentioned in some of the comments, CAST applies U.S. date formatting rules when parsing date strings. So an ambiguous value like 01/07/2011 will always be interpreted as January 7th.

Since the date format is known, I think the two simplest options are to either:

A) Iterate through the query and parse the date strings manually:

<cfloop query="yourQuery">
    <cfif LSIsDate(yourQuery.DateCol, "English (UK)")>
        <cfset yourQuery.DateCol[currentRow] = LSParseDateTime(yourQuery.DateCol, "English (UK)") />
    </cfif>
</cfloop>

-==OR

B) Change the underlying cell format as Sean suggested. Apply a U.S. format like mm/dd/yyyy so the returned strings will be correctly parsed by CAST. Or you could simply apply a non-ambiguous format like yyyy-mm-dd, which would sort correctly even as a string.

<!--- read in the workbook --->
<cfset sheet = spreadSheetRead("c:/path/file.xls")>
<!--- apply the new format and save back to disk --->
<cfset SpreadSheetFormatColumn(sheet, {dataFormat="yyyy-mm-dd"}, yourDateColumn)>
<cfset SpreadSheetWrite(sheet, "c:/path/file.xls", true)>
<cfspreadsheet action="read" query="yourQuery" src="c:/path/file.xls" sheet="1" rows="2-178" >
于 2012-03-21T04:39:18.357 回答
2

您应该能够SpreadsheetFormatColumn在日期列上使用dataformat设置来为该列设置正确的日期格式。

您可以在 ColdFusion 9 文档中找到更多信息SpreadsheetFormatColumn和设置:dataformat

SpreadsheetFormatColumn:http ://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6806.html

格式化选项:http ://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6747.html

于 2012-03-20T16:31:00.110 回答
1

我认为您想要做的是使用该setLocale()功能将语言环境设置为欧洲地区,然后使用该功能进行查询CAST()

<cfset setLocale("French (Standard)") />

<cfquery name="mySpreadsheet2" dbtype="query">
   select (CAST(date_field as DATE))   as mydate
     from mySpreadsheet order by mydate
</cfquery>

我刚刚尝试使用从数据库中选择的 VARCHAR(使用 Oracle TO_CHAR(date_field, 'DD/MM/YYYY'))并且它有效。不知道你是否会得到相同的结果<cfspreadsheet>

希望这可以帮助。

于 2012-03-21T00:06:38.610 回答