Data type conversion
71
If you use SQL to insert data into a database or in a WHERE clause to select data from a
database, and the database driver does not support ODBC-formatted dates, use the
DateFormat
function to convert the date-time value to a valid format for the driver. This rule also applies to
queries of queries.
For example, the following SQL statement uses the
DateFormat
function in a query of queries to
select rows that have MyDate values in the future:
<cfquery name="MyQofQQ" dbtype="query">
SELECT *
FROM DateQuery
WHERE MyDate >= '#DateFormat(Now())#'
</cfquery>
The following query of queries fails with the error message “Error: {ts is not a valid date,” because
the ColdFusion
Now
function returns an ODBC timestamp:
<cfquery name="MyQofQQ" dbtype="query">
SELECT *
FROM DateQuery
WHERE MyDate >= '#now()#'
</cfquery>
Using JavaCast with overloaded Java methods
You can overload Java methods so a class can have several identically named methods that differ
only in parameter data types. At runtime, the Java virtual machine attempts to resolve the specific
method to use, based on the types of the parameters passed in the call. Because ColdFusion does
not use explicit types, you cannot predict which version of the method the virtual machine will
use.
The ColdFusion
JavaCast
function helps you ensure that the right method executes by
specifying the Java type of a variable, as in the following example:
<cfset emp.SetJobGrade(JavaCast("int", JobGrade))>
The
JavaCast
function takes two parameters: a string representing the Java data type and the
variable whose type you are setting. You can specify the following Java data types: bool, int, long,
float, double, and String.
For more information on the
JavaCast
function, see
CFML Reference.
Using quotation marks
To ensure that ColdFusion properly interprets string data, surround strings in single- or double-
quotation marks. For example, ColdFusion evaluates “10/2/2001” as a string that can be
converted into a date-time object. However, it evaluates 10/2/2001 as a mathematical expression,
5/2001, which evaluates to 0.00249875062469.
Examples of type conversion in expression evaluation
The following examples demonstrate ColdFusion expression evaluation.
Example 1
2 * True + "YES" - ('y' & "es")
Result value as string: "2"
Explanation: (2*True) is equal to 2; ("YES"- "yes") is equal to 0; 2 + 0 equals 2.
Summary of Contents for COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Page 1: ...Developing ColdFusion MX Applications...
Page 22: ...22 Contents...
Page 38: ......
Page 52: ...52 Chapter 2 Elements of CFML...
Page 162: ......
Page 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Page 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Page 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Page 266: ......
Page 314: ...314 Chapter 14 Handling Errors...
Page 344: ...344 Chapter 15 Using Persistent Data and Locking...
Page 349: ...About user security 349...
Page 357: ...Security scenarios 357...
Page 370: ...370 Chapter 16 Securing Applications...
Page 388: ...388 Chapter 17 Developing Globalized Applications...
Page 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Page 410: ......
Page 426: ...426 Chapter 19 Introduction to Databases and SQL...
Page 476: ...476 Chapter 22 Using Query of Queries...
Page 534: ...534 Chapter 24 Building a Search Interface...
Page 556: ...556 Chapter 25 Using Verity Search Expressions...
Page 558: ......
Page 582: ...582 Chapter 26 Retrieving and Formatting Data...
Page 668: ......
Page 734: ...734 Chapter 32 Using Web Services...
Page 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Page 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Page 788: ......