SQL 数组作为数字返回,而不是 ColdFusion 中的字符串

发布于 2024-10-02 14:18:01 字数 3026 浏览 5 评论 0 原文

在 Coldfusion 中,我使用一个 cfc 将一个选择框绑定到另一个选择框(基本上,从一个框中选择一个州,第二个框填充县名称。)县框的值是一个 5 位数字,即格式化为文本(即值来自文本字段。)

问题是我发现如果所选县 ID 的值以“0”开头,则它已被截断。

所以我得到类似的东西: 艾达县 11223A 2300 B(应该是02300)

有人可以帮助确保前导0不会被切断吗?

这是页面上的选择框:

 <!--- State Name options --->
    <b>State:</b><br />
    <cfselect bind="cfc:states.getStates()" bindonload="true" name="search_state" id="search_state" value="StateUSAbb" display="StateName">
    </cfselect><br />

  <!--- County Name options --->
    <b>County:</b><br />
    <cfselect bind="cfc:states.getCounties({search_state})" name="search_county" id="search_county" value="FIPS_County" display="CountyName">
    </cfselect>

我讨厌粘贴整个 .cfc,但要注意后面的部分,特别是使用 cfset 填充数组的 cfloop 结果:

<cfcomponent output="false">

    <!--- Get array of media types --->
    <cffunction name="getStates" access="remote" returnType="array">
        <!--- Define variables --->
        <cfset var data="">
        <cfset var result=ArrayNew(2)>
        <cfset var i=0>

        <!--- Get data --->
        <cfquery name="data" datasource="bridges">
       SELECT DISTINCT tblLoc.StateUSAbb, lkuState.StateName
        FROM lkuState INNER JOIN tblLoc ON lkuState.FIPS_State = tblLoc.FIPS_State
        WHERE (lkuState.StateName <> 'New Brunswick')
        UNION
        SELECT '' AS StateUSAbb, ' ALL' AS StateName
        FROM lkuState
        ORDER BY StateName
        </cfquery>

        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[i][1]=data.StateUSAbb[i]>
            <cfset result[i][2]=data.StateName[i]>
        </cfloop>

        <!--- And return it --->
        <cfreturn result>
    </cffunction>

    <!--- Get counties by state --->
    <cffunction name="getCounties" access="remote" returnType="array">
        <cfargument name="stateabb" type="string" required="true">

        <!--- Define variables --->
        <cfset var data="">
        <cfset var result=ArrayNew(2)>
        <cfset var i=0>

        <!--- Get data --->
        <cfquery name="data" datasource="bridges">
        SELECT '' AS FIPS_COUNTY, ' ALL' as CountyName
        FROM lkuCnty
        UNION
        SELECT FIPS_County, CountyName
        FROM lkuCnty
        WHERE StateAbb = '#ARGUMENTS.stateabb#'
        ORDER BY CountyName
        </cfquery>

        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[i][1]=data.FIPS_County[i]>
            <cfset result[i][2]=data.CountyName[i]>
        </cfloop>

        <!--- And return it --->
        <cfreturn result>
    </cffunction>

</cfcomponent>

In Coldfusion, I'm using a cfc that binds one select box to another (basically, choose a State from one box, the second box is populated with County names.) The value for the County box is a 5-digit number WHICH IS FORMATTED AS TEXT (ie. the value comes from a text field.)

The problem is that I'm finding that if the value of the selected county id starts with a '0', it's been cut off.

So I get stuff like:
ID County
11223 A
2300 B (should be 02300)

Can someone help make sure that leading 0s are not cut off?

Here's the select boxes on the page:

 <!--- State Name options --->
    <b>State:</b><br />
    <cfselect bind="cfc:states.getStates()" bindonload="true" name="search_state" id="search_state" value="StateUSAbb" display="StateName">
    </cfselect><br />

  <!--- County Name options --->
    <b>County:</b><br />
    <cfselect bind="cfc:states.getCounties({search_state})" name="search_county" id="search_county" value="FIPS_County" display="CountyName">
    </cfselect>

I hate pasting the whole .cfc but pay attention to the latter part, particularly the cfloop which uses a cfset to populate array RESULT:

<cfcomponent output="false">

    <!--- Get array of media types --->
    <cffunction name="getStates" access="remote" returnType="array">
        <!--- Define variables --->
        <cfset var data="">
        <cfset var result=ArrayNew(2)>
        <cfset var i=0>

        <!--- Get data --->
        <cfquery name="data" datasource="bridges">
       SELECT DISTINCT tblLoc.StateUSAbb, lkuState.StateName
        FROM lkuState INNER JOIN tblLoc ON lkuState.FIPS_State = tblLoc.FIPS_State
        WHERE (lkuState.StateName <> 'New Brunswick')
        UNION
        SELECT '' AS StateUSAbb, ' ALL' AS StateName
        FROM lkuState
        ORDER BY StateName
        </cfquery>

        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[i][1]=data.StateUSAbb[i]>
            <cfset result[i][2]=data.StateName[i]>
        </cfloop>

        <!--- And return it --->
        <cfreturn result>
    </cffunction>

    <!--- Get counties by state --->
    <cffunction name="getCounties" access="remote" returnType="array">
        <cfargument name="stateabb" type="string" required="true">

        <!--- Define variables --->
        <cfset var data="">
        <cfset var result=ArrayNew(2)>
        <cfset var i=0>

        <!--- Get data --->
        <cfquery name="data" datasource="bridges">
        SELECT '' AS FIPS_COUNTY, ' ALL' as CountyName
        FROM lkuCnty
        UNION
        SELECT FIPS_County, CountyName
        FROM lkuCnty
        WHERE StateAbb = '#ARGUMENTS.stateabb#'
        ORDER BY CountyName
        </cfquery>

        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[i][1]=data.FIPS_County[i]>
            <cfset result[i][2]=data.CountyName[i]>
        </cfloop>

        <!--- And return it --->
        <cfreturn result>
    </cffunction>

</cfcomponent>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月下客 2024-10-09 14:18:02

在数字末尾添加一个空格,那么 CF 会将其视为字符串,并且不会截掉前导 0。

简单的解决方法:

顺便说一句,您知道支持查询对象来填充 cfselect 对吧?所以你甚至不需要循环。您可以执行相同的解决方法,但在 cfquery

UPDATE: 内的 SQL 中,无论如何,想法是,如果您想保留前导 0 并继续使用 CF 内置的 serializeJSON() 或调用 cfc 远程方法在 JSON 样式中(将在内部调用serializeJSON(),您可以附加一个空格,以便 CF 会将其视为字符串,并保留前导 0。如果您的脚本必须需要“012345”且没有尾随空格,则寻找另一个来自 riaforge 或 cflib 的 JSON 序列化器。

append a space to the end of the number, then CF will treat it as a string and no leading 0 will be chopped.

simple workaround: <cfset result[i][1]=data.StateUSAbb[i] & " ">

btw, you know that query object is supported for populating cfselect right? So you don't even need the loop. You can do the same workaround but in SQL inside your cfquery

UPDATE: anyway, the idea is that if u want to preserve the leading 0 and keep using CF's built in serializeJSON() or calling the cfc remote method in JSON style (which will internally invoke serializeJSON(), you can append a space so CF will treat it as a string and leading 0 will be preserved. If your script somehow must need "012345" with no trailing space, then look for another JSON seralizer from riaforge or cflib.

放血 2024-10-09 14:18:02

您确定从查询返回的数据是包含前导零的文本字符串,而不仅仅是整数值?无论如何,我认为 Zachary 对 NumberFormat(x, "00000") 的建议是正确的选择。

You're sure the data returned from your query is a text string which contains the leading zero, rather than just the integer value? Regardless, I think Zachary's suggestion of NumberFormat(x, "00000") is the way to go.

一个人的夜不怕黑 2024-10-09 14:18:01

如果数据是固定长度,您可以使用 NumberFormat 强制前导零。一般来说,CF 是无类型的,因此必须发生一些导致数据损坏的底层转换。您可以尝试强制值 toString(),或者调试添加单引号之类的内容作为列值中的第一个字符(例如 SELECT '''' + FIPS_County, '''' + CountyName FROM lkuCnty)以查看是否他们保留了所有的性格。

[更新]
根据您对 SQL 如何不返回 5 个字符的评论,使用此更新的查询从 INT 转换为带有前导零的 VARCHAR。

  SELECT DISTINCT 
    RIGHT('00000' + CONVERT(VARCHAR(5),StateUSAbb),5), 
    lkuState.StateName
         FROM lkuState INNER JOIN tblLoc ON lkuState.FIPS_State = tblLoc.FIPS_State
         WHERE (lkuState.StateName <> 'New Brunswick')
  UNION
     SELECT '' AS StateUSAbb,
     ' ALL' AS StateName
          FROM lkuState
          ORDER BY StateName

If the data is a fixed length you can use NumberFormat to force leading zero's. In general, CF is typeless so there must be some underlying conversion happening that is causing the data to get corrupt. You might try forcing the value toString(), or to debug add something like a single quote as the first character in the column value (eg. SELECT '''' + FIPS_County, '''' + CountyName FROM lkuCnty) to see if they keep all their characters.

[Update]
Based on your comments about how SQL is not returning 5 char, use this updated query to go from INT to VARCHAR with leading zeros.

  SELECT DISTINCT 
    RIGHT('00000' + CONVERT(VARCHAR(5),StateUSAbb),5), 
    lkuState.StateName
         FROM lkuState INNER JOIN tblLoc ON lkuState.FIPS_State = tblLoc.FIPS_State
         WHERE (lkuState.StateName <> 'New Brunswick')
  UNION
     SELECT '' AS StateUSAbb,
     ' ALL' AS StateName
          FROM lkuState
          ORDER BY StateName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文