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 »
10,640 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »