Search code examples
coldfusionqoq

Use count() function in ColdFusion query of query


I wanted to use count() function in ColdFusion Query object.

Here is my code and test:

<cfset x = querynew("id,name")>

<cfquery name="y" dbtype="query">
    select count(*) as total from x
</cfquery>

<cfoutput>Test1: #y.total#</cfoutput>

<cfset temp = QueryAddRow(x)>
<cfset Temp = QuerySetCell(x, "id", 1)>
<cfset Temp = QuerySetCell(x, "Name", "Vikas")>

<cfquery name="y" dbtype="query">
    select count(*) as total from x
</cfquery>

<cfoutput>Test2: #y.total#</cfoutput>

Should I use convert function? Like if total is [enpty string] then result should be 0. Or is there any other best/proper way to do that?


Solution

  • It does seem like this is a bug, however there is an easy way around it. Simply wrap the y.total in val(), so it would read:

    <cfoutput>Test1: #val(y.total)#</cfoutput> 
    <cfoutput>Test2: #val(y.total)#</cfoutput>
    

    val() will return 0 if an empty string is passed to it.