Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Minus sign" in variable name 1

Status
Not open for further replies.

alexerm

Programmer
Feb 27, 2003
17
0
0
CA
Hi,

I have a weird problem.

The table in the database has a column name with a "minus" sign ("mobile-phone").

When I'm trying to do the following...:
<cfoutput query=&quot;test&quot;>
#mobile-phone#
</cfoutpu>

..I'm getting error message from CF &quot;Invalid expression format&quot;. Probably it happens because it treats &quot;-&quot; as arithmetic operator.

Any ideas on how to resolve this problem without renaming columns in the database and without using &quot;SELECT mobile-phone AS mp......&quot; (too long to explain why I can not use it).

Thanks,

Alex
 
Okay, before I give you the answer, let me rant for a couple of sentences. Tell the powers that be that the SQL should be changed so you can alias Mobile-Phone to MobilePhone, or whatever. Workarounds aren't a good habit to get into. I won't suggest changing the column name because there might be a lot of other business logic that depends on the it staying the way it is (even though it isn't right).

Preaching aside, here is the way to get around this problem:
Code:
<cfoutput query=&quot;test&quot;>
#test[&quot;mobile-phone&quot;][CurrentRow]#
</cfoutput>
What this basically does is treat the query as a structure; since structure items can have characters in them that are considered illegal in regards to normal variable naming conventions, you can get away with it. The [CurrentRow] part of the code tells the cfoutput which line item to output.

I think this technique has been valid since CF 4.01, but don't hold me to that.

-Tek
 
Thanks a lot, Tek. Your suggestion with workaround helped me a lot.

The reason, why I did not want to use aliases, because I'm writing a tool, that takes table name as parameter and then does &quot;some&quot; stuff with data and then imports to other database. I posted my question in very simplified version. But in reality everything much more complicated.
Basically my &quot;tool&quot; does not know what column names exist in &quot;that&quot; table. I was going to write a separate query to get column names and then produce dynamic &quot;SELECT AS&quot; query with illigal signs removed. But workaround you showed me is much easier... Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top