ColdFusion Data Types From Different Sources (And How ColdFusion Sees Them)

Posted November 2, 2006 at 10:29 AM by Ben Nadel

Tags: ColdFusion

Most things in ColdFusion appear to be strings, but do you ever wonder what they are underneath? Yeah, me too. It's good to know as it can cause issue when sending data as arguments to Java methods or when attempting to use the underlying Java methods. I tried setting / paraming data in various scope to see how ColdFusion interpreted them.

VARIABLES Scope

I set up these values:

  • <cfset a = "0" />
  • <cfset b = 0 />
  • <cfparam name="c" type="numeric" default="0" />
  • <cfparam name="d" type="boolean" default="0" />
  • <cfparam name="e" type="date" default="#Now()#" />
  • <cfparam name="f" type="email" default="sarah@hotties.com" />
  • <cfparam name="g" type="eurodate" default="02/11/06" />
  • <cfparam name="h" type="float" default="0.0" />
  • <cfparam name="i" type="GUID" default="AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA" />
  • <cfparam name="j" type="integer" default="0" />
  • <cfparam name="k" type="ssn" default="105-00-1234" />
  • <cfparam name="l" type="telephone" default="2126911134" />
  • <cfparam name="m" type="time" default="09:00:00" />
  • <cfparam name="n" type="URL" default="http://www.bennadel.com" />
  • <cfparam name="o" type="XML" default="<dude>Ben</dude>" />

... and outputted the data types:

  • A: #a.GetClass().GetName()#
  • B: #b.GetClass().GetName()#
  • C: #c.GetClass().GetName()#
  • D: #d.GetClass().GetName()#
  • E: #e.GetClass().GetName()#
  • F: #f.GetClass().GetName()#
  • G: #g.GetClass().GetName()#
  • H: #h.GetClass().GetName()#
  • I: #i.GetClass().GetName()#
  • J: #j.GetClass().GetName()#
  • K: #k.GetClass().GetName()#
  • L: #l.GetClass().GetName()#
  • M: #m.GetClass().GetName()#
  • N: #n.GetClass().GetName()#
  • O: #o.GetClass().GetName()#

... and got the following output:

A: java.lang.String
B: java.lang.String
C: java.lang.String
D: java.lang.String
E: coldfusion.runtime.OleDateTime
F: java.lang.String
G: java.lang.String
H: java.lang.String
I: java.lang.String
J: java.lang.String
K: java.lang.String
L: java.lang.String
M: java.lang.String
N: java.lang.String
O: java.lang.String

FORM Scope

I wanted to see if it mattered to declare the actual scope. So, I set up these variables:

  • <cfset FORM.a = "0" />
  • <cfset FORM.b = 0 />
  • <cfparam name="FORM.c" type="numeric" default="0" />
  • <cfparam name="FORM.d" type="boolean" default="0" />
  • <cfparam name="FORM.e" type="date" default="#Now()#" />
  • <cfparam name="FORM.f" type="email" default="sarah@hotties.com" />
  • <cfparam name="FORM.g" type="eurodate" default="02/11/06" />
  • <cfparam name="FORM.h" type="float" default="0.0" />
  • <cfparam name="FORM.i" type="GUID" default="AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA" />
  • <cfparam name="FORM.j" type="integer" default="0" />
  • <cfparam name="FORM.k" type="ssn" default="105-00-1234" />
  • <cfparam name="FORM.l" type="telephone" default="2126911134" />
  • <cfparam name="FORM.m" type="time" default="09:00:00" />
  • <cfparam name="FORM.n" type="URL" default="http://www.bennadel.com" />
  • <cfparam name="FORM.o" type="XML" default="<dude>Ben</dude>" />

... and outputted the data types:

  • A: #FORM[ "a" ].GetClass().GetName()#
  • B: #FORM[ "b" ].GetClass().GetName()#
  • C: #FORM[ "c" ].GetClass().GetName()#
  • D: #FORM[ "d" ].GetClass().GetName()#
  • E: #FORM[ "e" ].GetClass().GetName()#
  • F: #FORM[ "f" ].GetClass().GetName()#
  • G: #FORM[ "g" ].GetClass().GetName()#
  • H: #FORM[ "h" ].GetClass().GetName()#
  • I: #FORM[ "i" ].GetClass().GetName()#
  • J: #FORM[ "j" ].GetClass().GetName()#
  • K: #FORM[ "k" ].GetClass().GetName()#
  • L: #FORM[ "l" ].GetClass().GetName()#
  • M: #FORM[ "m" ].GetClass().GetName()#
  • N: #FORM[ "n" ].GetClass().GetName()#
  • O: #FORM[ "o" ].GetClass().GetName()#

... and got the following output:

A: java.lang.String
B: java.lang.String
C: java.lang.String
D: java.lang.String
E: coldfusion.runtime.OleDateTime
F: java.lang.String
G: java.lang.String
H: java.lang.String
I: java.lang.String
J: java.lang.String
K: java.lang.String
L: java.lang.String
M: java.lang.String
N: java.lang.String
O: java.lang.String

CFQUERY Scope

I wanted to see what kind of data was coming out of the database, well rather, the Query of Queries database. So, I set up these variables:

  • <cfset qTemp = QueryNew( "id" ) />
  • <cfset QueryAddRow( qTemp ) />
  • <cfquery name="qTemp" dbtype="query">
  • SELECT
  • ( CAST( '0' AS BINARY ) ) AS a,
  • ( CAST( 0 AS BIGINT ) ) AS b,
  • ( CAST( 0 AS BIT ) ) AS c,
  • ( CAST( 0 AS DATE ) ) AS d,
  • ( CAST( 0 AS DECIMAL ) ) AS e,
  • ( CAST( 0 AS INTEGER ) ) AS f,
  • ( CAST( 0 AS TIME ) ) AS g,
  • ( CAST( 0 AS TIMESTAMP ) ) AS h,
  • ( CAST( 0 AS VARCHAR ) ) AS i
  • FROM
  • qTemp
  • </cfquery>

... and outputted the data types:

  • A: #qTemp[ "a" ][ 1 ].GetClass().GetName()#
  • B: #qTemp[ "b" ][ 1 ].GetClass().GetName()#
  • C: #qTemp[ "c" ][ 1 ].GetClass().GetName()#
  • D: #qTemp[ "d" ][ 1 ].GetClass().GetName()#
  • E: #qTemp[ "e" ][ 1 ].GetClass().GetName()#
  • F: #qTemp[ "f" ][ 1 ].GetClass().GetName()#
  • G: #qTemp[ "g" ][ 1 ].GetClass().GetName()#
  • H: #qTemp[ "h" ][ 1 ].GetClass().GetName()#
  • I: #qTemp[ "i" ][ 1 ].GetClass().GetName()#

... and got the following output:

A: [B
B: java.lang.Long
C: java.lang.Integer
D: java.sql.Date
E: java.math.BigDecimal
F: java.lang.Integer
G: java.sql.Time
H: java.sql.Timestamp
I: java.lang.String

As you can see, pretty much all ColdFusion-defined variables are stored as strings (for simple values). For some reason, it stored DATE as a DATETIME object, but NOT TIME as a DATETIME object... go figure. On the other side of the wall, all the data that comes out of a database is very Java data type oriented.

Good stuff to know.



Reader Comments

Nov 2, 2006 at 3:11 PM // reply »
2 Comments

Also useful to know that if you were to do something like:

<cfset c = c + 1/>

And then call c.GetClass().GetName(), you'd end up with java.lang.Double . It's caught me out a couple of times when passing values into java methods that are of type int and obviously I was passing a double without realising.


Nov 3, 2006 at 7:21 AM // reply »
11,241 Comments

James,

Nice catch! Yeah, that is why I now try to make it a habit of always always always using JavaCast() when I am sending data to a Java method. The problem is that so often, it will take a string just fine and I have gotten lazy about using it for things like ReplaceAll() which work fine like all the time.


jM
Mar 25, 2007 at 6:09 PM // reply »
9 Comments

For some reason, it stored DATE as a DATETIME object, but NOT TIME as a DATETIME object

Not exactly. They were converted to java.sql.Date and java.sql.Time. I think you're confusing database data types and java.sql types.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 22, 2013 at 12:44 PM
Ask Ben: Query Loop Inside CFScript Tags
In cf10, if you call a function that has: local.result = {}; local.result.msg = ""; local.svc = new query(); local.svc.setSQL("SELECT * FROM..."); local.obj = local.svc.exe ... read »
May 22, 2013 at 12:29 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben: What version of Java are you using? Also, did you test users.id to see what Java reports as the data type? I wonder if it's not a Java primitive data type, but getting returned as something ... read »
May 22, 2013 at 11:47 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dana, Awesome - so it looks like this bug was fixed in ColdFusion 10. Thanks so much for double-checking that. ... read »
May 22, 2013 at 11:37 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
When I c&p and run on cf10, I get: Selected User IDs: 1,4 User 1 selected: YES - YES User 2 selected: NO - NO User 3 selected: NO - NO User 4 selected: YES - YES User 5 selected: NO - ... read »
May 22, 2013 at 11:27 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Tom, Good thought, but no dice. Both of these still exhibit the same behavior: users.id[ users.currentRow ] users[ "id" ][ users.currentRow ] It's just something whacky happening with ... read »
May 22, 2013 at 11:07 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
Could your problem be that "users.id" is actually an ARRAY, not a single value? Perhaps try it again with "users.id[1]" (I only have CF8 here at work). ... read »
May 22, 2013 at 7:52 AM
Nested Views, Routing, And Deep Linking With AngularJS
Hi, Just a quick thank you. As it happens, for my own purposes, the pending ui-router work being done in native angular is likely the one I'll adopt, but your exploration, code and documentation of ... read »
May 22, 2013 at 4:43 AM
How Do You Use The ColdFusion CFParam Tag?
'<cfparam>' or 'isDefined()and <cfset>' performs the same task.Is there any difference? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools