query of query and modifying column data

I normally don't use the query of query functionality built into ColdFusion. But, for the project I am currently working on (top secret as it is at this point in time), I need to use query of query.

So, in my first (real) query from the database I returned four columns with about 400 records. This is what an interface I am working through actually runs in the background. The query looked something like this:

SELECT id, name, title, location
FROM members

I needed to remove a few characters from the end of the "title" field because it had a ";1" or ";0" appended to the end of it (don't ask) based on whether or not they were a manager. So, I wrote a query of query to do this.

<cfquery name="qGetMembers" dbtype="query">
SELECT id, name, left(title,(character_length(title) - 2)) title, location
FROM qGetInitialMembers
WHERE location = 'MN'
</cfquery>

I get a really helpful error: Encountered "(. Incorrect Select Statement, Expecting a 'FROM', But Encountered '(' Instead, A Select Statement Should Have a 'FROM' Construct. What does that mean? I had no idea. Until I did some research. In what I read, it looks like in a query of query, you cannot alter the data of a column (cell). You can add to the query though. So, to return the title how I wanted it, I used this code:

<cfquery name="qGetMembers" dbtype="query">
SELECT id, name, title, location
FROM qGetInitialMembers
WHERE location = 'MN'
</cfquery>

<!--- create an array and populate it with values for the title --->
<cfset arrTitleFinal = arrayNew(1) />

<cfloop query="qGetMembers">
<cfset arrayAppend(arrTitleFinal,left(qGetMembers.title,(len(qGetMembers.title) - 2))) />
</cfloop>

<!--- add the new column to the query with values --->
<cfset queryAddColumn(qGetMembers,"title_final","VARCHAR",arrTitleFinal) />

So now the query of query qGetMembers returns four columns with a new one of title_final. You could even take this one step further (which I did) and do one more query of query to return the "title_final" column as "title" for consistency, but this works as is! This took me about four hours to figure out. Now that I know this, I definitely won't forget it, and I hope this helps someone out too, so they don't have to waste alot of time on the wonderful query of query.

Comments
joel's Gravatar Maybe a silly question, but why didn't you just do the parse in the original query itself instead of modifying it after the fact?

SELECT id, name, left(title,(character_length(title) - 2)) title, location
FROM members
# Posted By joel | 1/3/08 3:40 PM
johann's Gravatar @joel

As I alluded to in my post, I am interfacing with a different technology written in Java. I just get the recordset from the Java program, in the format specified. I can't rewrite what that program gives me, so I have to do the query of query.
# Posted By johann | 1/3/08 4:01 PM
Sana's Gravatar Why not simply do this when you try to display the recordset.

<cfoutput query="qGetMembers">
left(qGetMembers.title[CurrentRow],(len(qGetMembers.title[CurrentRow]) - 2))
</cfoutput>

easy peasy.....
# Posted By Sana | 1/3/08 5:57 PM
johann's Gravatar @Sana

Yup, I could have done it in the "view", but the application I am working on is in Model-Glue and in sticking with the MVC model, I thought that I should put that logic in the Gateway (model) of my application, otherwise, yes, you are correct, it would be really easy to do in the view!
# Posted By johann | 1/3/08 10:09 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.7.002.