Can we append a DateTime to an array with the ArrayAppend() function of ColdFusion? - sql

ERROR:Can't cast Object type [DateTime] to a value of type [Array]
<cfset Seniority=ArrayNew(1)>
<CFLOOP QUERY="all_employees">
<cfif isNull(all_employee.TimeInPositionDate) >
<cfset ArrayAppend(Seniority,all_employee.hiredate)>
<cfelse>
<cfset ArrayAppend(Seniority,all_employee.TimeInPositionDate)>
</cfif>
</CFLOOP>

Your issue stems form the variable lookup in CFML. While you're inside a query loop, ColdFusion will pull from the query scope before your variables scope. Due to there being a column in your query also called Seniority your code reads the same as ArrayAppend(all_employees.Seniority,all_employees.hiredate);
Changing the name of your array will solve the issue at hand.
<cfset all_employees = queryNew(
"Id,TimeInPositionDate,Hiredate,Seniority",
"Integer,Timestamp,Timestamp,Timestamp",
[
{"Id":1,"HireDate":Now(),"Seniority":Now()},
{"Id":2,"HireDate":Now(),"TimeInPositionDate":Now(),"Seniority":Now()}
]
)>
<cfset arrSeniority=ArrayNew(1)>
<CFLOOP QUERY="all_employees">
<cfif isNull(all_employees.TimeInPositionDate) >
<cfset ArrayAppend(arrSeniority,all_employees.hiredate)>
<cfelse>
<cfset ArrayAppend(arrSeniority,all_employees.TimeInPositionDate)>
</cfif>
</CFLOOP>
<cfdump var="#arrSeniority#"/>

I still think looping is much easier in CFScript (not to mention the fact that it just looks cleaner to me. Anyway, I can knock this down to essentially one line of code (beyond the array creation and looping code).
First, I set up my fake query object:
all_employees = QueryNew(
"id, TimeInPositionDate, hiredate" ,
"integer, date, date" ,
[
{
id:1 ,
TimeInPositionDate: "2018-01-01" ,
hiredate: "2017-01-01"
} ,
{
id:2 ,
// TimeInPositionDate: Not defined, so NULL
hiredate: "2017-02-01"
} ,
{
id:3 ,
TimeInPositionDate: "2018-03-01"
//hiredate: Not defined, so NULL
} ,
{
id:4
//TimeInPositionDate: Not defined, so NULL
//hiredate: Not defined, so NULL
}
]
);
I have created 3 rows. 1 full row, 1 with just a TimeInPositionDate, 1 with just a hiredate and 1 with neither. ColdFusion has always been a little odd with how it treats null, and it doesn't always mesh up well with a SQL query. In our fake query, we just don't define the rows we want to be null.
Next I create my array:
Seniority = [] ;
We don't have to use ArrayNew() to make an array. We can just use implicit notation to be much shorter.
Next we use a for/in loop around our query to append row data to our new array.
for (r in all_employees) {
Seniority.append( len(r.TimeInPositionDate) ? r.TimeInPositionDate : r.hiredate ) ;
}
Here, we're using the append() member function to add to our Seniority array. We also use a ternary operation to pick which value we want to add. It basically says that if there is a length to TimeInPositionDate for that row, then we use the first condition (TimeInPositionDate), otherwise we use hiredate.
I though about using an Elvis Operator...
Seniority2.append( q.TimeInPositionDate ?: q.hiredate ) ;
..., but since the query is likely returning q.TimeInPositionDate="" instead of an actual null value, then the first value is technically defined and gets chosen. That would work on a true null, but an empty string isn't a null.
You could probably also use each() or some other loopish function to iterate through your query object, but I've found for/in loops to usually be faster in cases like this. You'll have to test.
You can play with the full thing at:
https://cffiddle.org/app/file?filepath=de107907-234c-4df8-9386-02a288e53313/fdf21791-6cc4-4c55-b923-ad7726efa8ee/b3b6b906-8e3a-4f6b-9ec8-65845efc40f1.cfm
EDIT: NULLs can be so much fun. If we drop back into some Java, we can get an actual null value from the query rather than the cfquery-converted empty string, and show that Elvis is still in the building.
all_employees.getDate("TimeInPositionDate") ?: all_employees.getDate("hiredate") ) ;
Since Java's getDate() on a query will get the actual value and can handle a null, that correctly chooses the 2nd option for rows where TimeInPositionDate is null. That said, while I do appreciate the integration of Java and ColdFusion, I do not recommend it in this case. It's doing a lot of mixing of Java into the CF and isn't necessary beyond the demonstration.

Related

Can we Use "Case" in a ColdFusion Query-of-Query

I am applying case in ColdFusion query of query but it's throwing an error.
Query:
<cfquery name="qEmployees1" dbtype="query">
select (
case
when ISNUMERIC(u.userdefined)=1
then right('00000000'+u.userdefined,8)
else userdefined
end
) as hello
from all_employees
order by hello ASC
</cfquery>
Error message:
Encountered "when" at line 3, column 22. Was expecting one of:
"AND" ... "BETWEEN" ... "IN" ... "IS" ... "LIKE" ... "NOT" ...
"OR" ... ")" ... "=" ... "." ... "!=" ... "#" ... "<>" ...
">" ... ">=" ... "<" ... "<=" ... "+" ... "-" ... "*" ...
"||" ... "/" ... "**" ... "(" ...
Update:
The original suggestion isn't going to work due to it only looking at a single row. Really you need to loop through your all_employees recordset and apply it to each individual row.
You might be able to achieve this without QoQ if you are just outputting the results to the page. Like this:
<cfoutput>
<cfloop query="all_employees">
<cfif isNumeric(all_employees.userdefined)>
#Right('00000000'&all_employees.userdefined,8)#
<cfelse>
#all_employees.userdefined#
<cfif>
</cfloop>
</cfoutput>
Original Answer:
How about something like this?:
<cfquery name="qEmployees1" dbtype="query">
SELECT
<cfif isNumeric([all_employees].[u.userdefined])>
right('00000000'+u.userdefined,8)
<cfelse>
u.userdefined
</cfif> AS hello
FROM all_employees
ORDER by hello
</cfquery>
I have not tested this but I don't think having dot notation in the SQL column name will work correctly in this case. I enclosed it in square brackets anyway.
In case anyone else decides to try the QoQ below, one very important thing to note is that even if it executes without error, it's NOT doing the same thing as CASE. A CASE statement applies logic to the values within each row of a table - individually. In the QoQ version, the CFIF expression does not operate on all values within the query. It only examines the value in the 1st row and then applies the decision for that one value to ALL rows in the query.
Notice how the QoQ below (incorrectly) reports that all of the values are numeric? While the database query (correctly) reports a mix of "Numeric" and "Non-numeric" values. So the QoQ code is not equivalent to CASE.
TestTable Data:
id userDefined
1 22
2 AA
3 BB
4 CC
Database Query:
SELECT CASE
WHEN ISNUMERIC(userDefined)=1 THEN 'Number: '+ userDefined
ELSE 'Not a number: ' + userDefined
END AS TheColumnAlias
FROM TestTable
ORDER BY ID ASC
Database Query Result:
QoQ
<cfquery name="qQueryOfQuery" dbtype="query">
SELECT
<cfif isNumeric(qDatabaseQuery2.userDefined)>
'Number: '+ userDefined
<cfelse>
'Not a number: ' + userDefined
</cfif>
AS TheColumnAlias
FROM qDatabaseQuery2
ORDER by ID
</cfquery>
QoQ Result
EDIT:
I thought about this one and decided to change it to an actual answer. Since you're using CF2016+, you have access to some of the more modern features that CF offers. First, Query of Query is a great tool, but it can be very slow. Especially for lower record counts. And then if there are a lot of records in your base query, it can eat up your server's memory, since it's an in-memory operation. We can accomplish our goal without the need of a QoQ.
One way we can sort of duplicate the functionality that you're looking for is with some of the newer CF functions. filter, each and sort all work on a query object. These are the member function versions of these, but I think they look cleaner. Plus, I've used cfscript-syntax.
I mostly reused my original CFSCript query (all_employees), that creates the query object, but I added an f column to it, which holds the text to be filtered on.
all_employees = QueryNew("userdefined,hello,f", "varchar,varchar,varchar",
[
["test","pure text","takeMe"],
["2","number as varchar","takeMe"],
["03","leading zero","takeMe"],
[" 4 ","leading and trailing spaces","takeMe"],
["5 ","extra trailing spaces","takeMe"],
[" 6","extra leading spaces","takeMe"],
["aasdfadsf","adsfasdfasd","dontTakeMe"],
["165e73","scientific notation","takeMe"],
["1.5","decimal","takeMe"],
["1,5","comma-delimited (or non-US decimal)","takeMe"],
["1.0","valid decimal","takeMe"],
["1.","invalid decimal","takeMe"],
["1,000","number with comma","takeMe"]
]
) ;
The original base query didn't have a WHERE clause, so no additional filtering was being done on the initial results. But if we needed to, we could duplicate that with QueryFilter or .filter.
filt = all_employees.filter( function(whereclause){ return ( whereclause.f == "takeMe"); } ) ;
This takes the all_employees query and applies a function that will only return rows that match our function requirements. So any row of the query where f == "takeMe". That's like WHERE f = 'takeMe' in a query. That sets the new filtered results into a new query object filt.
Then we can use QueryEach or .each to go through every row of our new filtered query to modify what we need to. In this case, we're building a new array for the values we want. A for/in loop would probably be faster; I haven't tested.
filt.each(
function(r) {
retval.append(
ISNUMERIC(r.userDefined) ? right("00000000"&ltrim(rtrim((r.userdefined))),8) : r.userDefined
) ;
}
) ;
Now that we have a new array with the results we want, the original QoQ wanted to order those results. We can do this with ArraySort or .sort.
retval.sort("textnocase") ;
In my test, CF2016 seemed to pass retval.sort() as a boolean and didn't return the sorted array, but CF2018 did. This was expected behavior, since the return type was changed in CF2018. Regardless, both will sort the retval array, so that when we dump the retval array, it's in the chosen order.
And as I always suggest, load test on your system with your data. Like I said, this is only one way to go about what you're trying to do. There are others that may be faster.
https://cffiddle.org/app/file?filepath=dedd219b-6b27-451d-972a-7af75c25d897/54e5559a-b42e-4bf6-b19b-075bfd17bde2/67c0856d-bdb3-4c92-82ea-840e6b8b0214.cfm
(CF2018) > https://trycf.com/gist/2a3762dabf10ad695a925d2bc8e55b09/acf2018?theme=monokai
https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/queryfilter.html
https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/queryeach.html
https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-a-b/arraysort.html
ORIGINAL:
This is more of a comment than an answer, but it's much too long for a comment.
I wanted to mention a couple of things to watch out for.
First, ColdFusion's isNumeric() can sometimes have unexpected results. It doesn't really check to see if a value is a number. It checks if a string can be converted to number. So there are all sorts of values that isNumeric() will see as numeric. EX: 1e3 is scientific notation for 1000. isNumeric("1e3") will return true.
My second suggestion is how to deal with leading and trailing space in a "numeric" value, EX: " 4 ". isNumeric() will return true for this one, but when you append and trim for your final value, it will come out as "000000 4". My suggestion to deal with these is to use val() or ltrim(rtrim()) around your column. val() will reduce it to a basic number (" 1.0 " >> "1") but ltrim(rtrim()) will retain the number but get rid of the space (" 1.0 " >> "1.0") and also retain the "scientific notation" value (" 1e3 " >> "1e3"). Both still miss 1,000, so if that's a concern you'll need to handle that. But the method you use totally depends on the values your data contains. Number verification isn't always as easy as it seems it should be.
I've always been a firm believer in GIGO -- Garbage In, Garbage Out. I see basic data cleansing as part of my job. But if it's extreme or regular, I'll tell the source to fix it or their stuff won't work right. When it comes to data, it's impossible to account for all possibilities, but we can check for common expectations. It's always easier to whitelist than it is to blacklist.
<cfscript>
all_employees = QueryNew("userdefined,hello", "varchar,varchar",
[
["test","pure text"],
["2","number as varchar"],
["03","leading zero"],
[" 4 ","leading and trailing spaces"],
["5 ","extra trailing spaces"],
[" 6","extra leading spaces"],
["165e73","scientific notation"],
["1.5","decimal"],
["1,5","comma-delimited (or non-US decimal)"],
["1.0","valid decimal"],
["1.","invalid decimal"],
["1,000","number with comma"]
]
)
//writedump(all_employees) ;
retval = [] ;
for (r in all_employees) {
retval.append(
{
"1 - RowInput" : r.userdefined.replace(" ","*","all") , // Replace space with * for output visibility.
"2 - IsNumeric?" : ISNUMERIC(r.userdefined) ,
"3 - FirstOutput": ( ISNUMERIC(r.userDefined) ? right("00000000"&r.userdefined,8) : r.userDefined ) ,
"4 - ValOutput" : ( ISNUMERIC(r.userDefined) ? right("00000000"&val(r.userdefined),8) : r.userDefined ) ,
"5 - TrimOutput" : ( ISNUMERIC(r.userDefined) ? right("00000000"&ltrim(rtrim((r.userdefined))),8) : r.userDefined )
}
) ;
}
writeDump(retval) ;
</cfscript>
https://trycf.com/gist/03164081321977462f8e9e4916476ed3/acf2018?theme=monokai
What are you trying to do exactly? Please share some context of the goal for your post.
To me it looks like your query may not be formatted properly. It would evalusate to something like:
select ( 0000000099
) as hello
from all_employees
order by hello ASC
Try doing this. Put a <cfabort> right here... And then let me know what query was produced on the screen when you run it.
<cfquery name="qEmployees1" dbtype="query">
select (
case
when ISNUMERIC(u.userdefined)=1
then right('00000000'+u.userdefined,8)
else userdefined
end
) as hello
from all_employees
order by hello ASC
<cfabort>
</cfquery>
<cfquery name="qEmployees1" dbtype="query">
SELECT
(
<cfif isNumeric(all_employees.userdefined)>
right('00000000'+all_employees.userdefined,8)
<cfelse>
all_employees.userdefined
</cfif>
) AS hello
FROM all_employees
ORDER by hello
</cfquery>
it is the syntax free answer thanks to #volumeone

SQL query WHERE column IN (#list#) not returning any results

I have the next simple query :
SELECT code, description
FROM table
WHERE code in ( #list# )
The list is created from an XML feed with listAppend():
<cfset list= listAppend(list, data.data1[i].xmltext )>
<cfset qualifiedList1 = ListQualify(list, "'")>
With listQualify I wrap every element from the list in quotation marks for the query. The problem is that when I run the query, I don't get any results back.
If I dump the list the query look like this :
SELECT code, description
FROM table
WHERE code in ('''BG/NN1'',''BG/NL2'',''BG/NN3'',''BG/NN4'',''BG/NN5''')
Any ideas on how can fix this problem?
Update 1:
I've fixed the problem.The problem was with ListQualify(list, "'")> Because list Qualify wraps every element in quotes the list attribute from cfqueryparam didn't recognized any of the values.Thank you!

ColdFusion: SQL Select IN from a Query

I have a SQL Server query which returns two values for one MyBusinessUnit column returns two values, say:
1111
2222
in a query object called MyQuery1
Both of these values also exist in a DB2 database's MyCorpUnit column.
What I want is to select all the matching records from the DB2 table--and, no, cross database queries are NOT working.
So, here's my Query2 for DB2 database:
<cfquery name="Query2" datasource="#application.DSN#">
SELECT MyCorpUnit WHERE MyCorpUnit IN
(
<cfqueryparam value=" #Query1.MyBusinessUnit #" CFSQLType="cf_sql_varchar" />
)
</cfquery>
But Query2 only returning matching record for only one value (1111).
So some other approach is needed. I have tried to create a string but that didn't work either.
Any idea?
Thanks!
cfqueryparam has a list attribute which might help:
<cfqueryparam value = "parameter value"
CFSQLType = "parameter type"
list = "yes|no"
maxLength = "maximum parameter length"
null = "yes|no"
scale = "number of decimal places"
separator = "separator character">
AND/OR ...additional criteria of the WHERE clause...>
I've used it before but not sure if it was in a QoQ or not. :D
ref: http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f6f.html
I am going to accept #AJ Dyka answer [Thank you!] but I have more to add to make it complete. Indeed, per his advice, I ended up using the 'LIST' attribute. A good discussion on it can be found here.
But, as you can see in the comments, I was still getting only "1111" despite using a List. And that's because of the leading spaces in my data. I ended up using a TRIM function. Here is a code snippet.
Converted the output from Query1 to a List :
<cfset ListUniqueWStreamBusinessUnit = ValueList(Query1.MyBusinessUnit )>
Then the magical code snippet!
...
WHERE trim(GMMCU) IN
(
<cfqueryparam value="#ListUniqueWStreamBusinessUnit#"
CFSQLType="cf_sql_varchar"
list="yes" />
)

How do we say when the value of the variable is placed where the variable is? What's the name of this?

For homework I'm writing a report and don't know what this is called: when the value of the variable is substituted into the statment.
Simple example:
var weekDay = Thursday
print "Today is "+ Weekday
In words I want to say 'print "Today is "+Weekday is _____ to "Today is Thursday"'
What goes where _____ is?
Actual example
<cfset aTable = "bookings">
<cfquery name="client" datasource="accounts">
SELECT * FROM #aTable#
</cfquery>
What is the word the describes the way this is related to the statement
<cfset aTable = "bookings">
<cfquery name="client" datasource="accounts">
SELECT * FROM bookings
</cfquery>
Would you say "after the value of the variable is substituted into its identifier"?
"Today is " + Weekday evaluates to "Today is Thursday"
If the substitution is done automatically by recognizing variable references inside a string, a common term is "interpolation".
If you perform the substitution using an explicit operator like "+" in Javascript or "." in Perl or PHP, it's called "concatenation".

fixing "more columns in insert than values specified"

A user got this error
DIAGNOSTICS: Error Executing Database Query. [Macromedia][SQLServer
JDBC Driver][SQLServer]There are more columns in the INSERT statement
than values specified in the VALUES clause. The number of values in
the VALUES clause must match the number of columns specified in the
INSERT statement. The error occurred on line 252. MESSAGE: Error
Executing Database Query. ROOT CAUSE:
coldfusion.tagext.sql.QueryTag$DatabaseQueryException: Error Executing
Database Query. Server: server2
There should be around 60 values/columns. Possible solution is adding <CFIF isDefined("VARIABLES.xxx")> to each of the values in INSERT and VALUES but this would take a long time. Is there a better way? Here is some of the code
<CFQUERY name="addAgency" datasource="#REQUEST.dsn#">
INSERT INTO agency (agency_name, code, address1, address2, city_id, postal, phone, alternativePhone, fax, provincialRegistration,
<!--- contact, ---> firstname, lastname, username, password, relationship_id, editdate, adddate, email, URL_link,Airport_id,
header, teaser, full_desc, <CFIF isDefined("VARIABLES.specialty")>specialty,</CFIF> hours_1, hours_2, hours_3, hours_4,
hours_5, hours_6, hours_7, merchant_ID,
region_ID, fcApproved, agencyType_ID, agencystatus, crossview, insp_Approved, rbc_Approved,
branch_number, gp_entity_ID,
<CFIF VARIABLES.alias1 gt 100> alias1,</CFIF>
<CFIF VARIABLES.alias2 gt 100> alias2,</CFIF>
<CFIF Trim(VARIABLES.alias3) NEQ ""> alias3,</CFIF>
default_agent_id,LOCAL_PASSWORD,LOCAL_USERNAME,NATIONAL_PASSWORD,NATIONAL_USERNAME,VACATIONCLUB_PASSWORD,VACATIONCLUB_USERNAME,toll_free_number,display_toll_free ,MARKETINGREGIONID, searchPreference, bookingOption,customer_can_choose
<CFIF isDefined("variables.logo_filedata")>,logo_fileName,logo_fileData,logo_mimeType</CFIF>
,gds, pseudo_city, acv_affiliate_code, vip_ext,owner_manager_title_en,owner_manager_title_fr)
VALUES (<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.agency_name))#" cfsqltype = "cf_sql_varchar" maxlength="150">,
<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.code))#" cfsqltype = "cf_sql_varchar" maxlength="50">,
<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.address1))#" cfsqltype = "cf_sql_varchar" maxlength="255">,
<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.address2))#" cfsqltype = "cf_sql_varchar" maxlength="255">,
<CFQUERYPARAM value="#sanitize(VARIABLES.city_id,true,true,false,true,false)#" cfsqltype = "cf_sql_integer">,
<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.postal))#" cfsqltype = "cf_sql_varchar" maxlength="10">,
<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.phone))#" cfsqltype = "cf_sql_varchar" maxlength="20">,
<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.fax))#" cfsqltype = "cf_sql_varchar" maxlength="20">,
<!--- <CFIF IsDefined("VARIABLES.contact")>#Trim(VARIABLES.contact)#, <CFELSE> ' ', </CFIF> --->
<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.firstname))#" cfsqltype = "cf_sql_varchar" maxlength="40">,
<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.lastname))#" cfsqltype = "cf_sql_varchar" maxlength="40">,
'-', '-',
<!--- Dummy un/pw --->
<CFQUERYPARAM value="#sanitize(VARIABLES.relationship_ID,true,true,false,true,false)#" cfsqltype = "cf_sql_integer">,
#CreateODBCDateTime(Now())#,
#CreateODBCDateTime(Now())#,
<CFQUERYPARAM value="#Trim(sanitize(VARIABLES.email))#" cfsqltype = "cf_sql_varchar" maxlength="40">,
Each of your s in the "columns" area MUST be matched with corollary CFIF in the "values" section as long as you are doing it this way. While there are other ways of doing it, they are not necessarily less messy. You could, for example:
set defaults for all columns with some cfparams so you did not have to have any "cfifs"
Write multiple "cleaner" queries
Move the whole thing to a stored procedure and pass what you have into it - the SP would then worry about the logic.
Save a small subset of values then "update" based on your cfif logic (don't like that one!).
As you can see they all have a downside.
Elaboration on 1)
Instead of doing something arduous like:
<CFIF isDefined("VARIABLES.specialty")> Specialty,</cfif>
Before you start your query make sure you have a value for all the variables you need using cfparam as in:
<Cfparam name="Specialty" default=""/>
(of course the default for specialty might be 0 or whatever).
The cfparam tag checks to see IF the variable exist and if it does not exist it creates it with the default you specify. This would mean you can "guarantee" that the variable will indeed exist - meaning you can eliminate the CFIF statement in your query.
One cavaet has to do with NULLs in the database. If you are depending on nulls (meaning if specialty does not exist you wish for the column to remain null), then you will need to add the "null" attribute to your cfqueryparam as in:
<CFQUERYPARAM
value="#Trim(sanitize(VARIABLES.specialty))#"
cfsqltype = "cf_sql_varchar"
null="#mynullfunction(variables.specialty)#"/>,
Note the "mynullfunction" - I ususally create a utility UDF for this that simply returns YES if the value is blank or zero (or whatever I define) and "no" if it's populated.
The best solution is to use the null attribute of cfqueryparam for columns where a default has not be set in the database, this means you can avoid adding conditions to the INSERT INTO and keeps the VALUE to one line per column. When a default does exist for a column then you need to use the long winded condition. This is the simplest, least obtrusive change.
Example where Alias1 has a database default, speciality does not.
` <cfset Variables.IsValidAlias1 = Variables.alias1 gt 100>
<cfquery name="addAgency" datasource="#REQUEST.dsn#">
INSERT INTO agency (agency_name,
specialty
<cfif Variables.IsValidAlias1>
, Alias1
</cfif>)
VALUES (<cfqueryparam value="#Variables.agency_name#" cfsqltype="cf_sql_varchar" maxlength="150">,
<cfqueryparam value="#Variables.specialty#" cfsqltype="cf_sql_varchar" null="#StructKeyExists(Variables,'Specialty') EQ false#" maxlength="199">
<cfif Variables.IsValidAlias1>
,<cfqueryparam value="#Variables.Alias1#" cfsqltype="cf_sql_varchar" maxlength="199">
</cfif>)
</cfquery>`
As an aside you should use StructKeyExists() instead of IsDefined as the former is faster and can be clearer to read. You should put this code inside a function, and that function inside a component so it can be re-used. This is assuming your using CF6 or later.
I would avoid setting empty string defaults for each field or setting values to NULL for non-existent keys. The reason is that you may now (or decide to have in the future) default values in your database columns. As the resulting NULL values would be explicit, they would override your database defaults. If you had passed in an empty string, this might be valid but as a default it likely is not.
My main advice would be to be keep each column on one line so that you easily see that you are being consistent in your conditionals. As Mark said, you must be careful to have the exact same conditionals in the "columns" area as the "values" area of your query.
Somewhat as an aside, I have a free tool called DataMgr that you could use to manage all of this sort of thing for most simple queries.
http://www.bryantwebconsulting.com/docs/datamgr/replace-sql-inserts-and-updates.cfm
http://www.bryantwebconsulting.com/docs/datamgr/getting-started.cfm
The main thing, however, is to be consistent in your conditionals and to format your query in such a way as to make it obvious when you are not.