<?xml version="1.0" encoding="utf-8"?>
			
			<rss version="2.0">
			<channel>
			<title>WebDH blog - SQL</title>
			<link>http://blog.webdh.com/index.cfm</link>
			<description>This is Troy&apos;s blog for WebDH.com LLC.</description>
			<language>en-us</language>
			<pubDate>Mon, 06 Sep 2010 13:11:11 -0700</pubDate>
			<lastBuildDate>Sat, 20 Mar 2010 17:16:00 -0700</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>webdh.com@gmail.com</managingEditor>
			<webMaster>webdh.com@gmail.com</webMaster>
			
			<item>
				<title>Using ColdFusion with a stubborn MS Access Date/Time field</title>
				<link>http://blog.webdh.com/index.cfm/2010/3/20/Using-ColdFusion-with-a-stubborn-MS-Access-DateTime-field</link>
				<description>
				
				I have a small application using a MS Access database. Yes, I know all the reasons why it shouldn&apos;t be used, let&apos;s not go there. Fact is, many developers still use Access for small apps, prototyping, etc, and may come across the same roadblock in which I figured out a solution. For reference, today&apos;s date is &lt;strong&gt;3/19/2010&lt;/strong&gt; which was used in the query examples shown.
&lt;br /&gt;&lt;br /&gt;

&lt;strong&gt;Requirement:&lt;/strong&gt; Query a table of Jobs (JobTitle, Dept, Salary, etc) that contains a Date/Time field named PostingEndDate. This field is configured as Required = No. That means the admin user who populates records in the table will either supply a date, or may leave the date blank.  Think of it as a field that allows NULL in MS SQL. On the end-user side, the query needs to list only current jobs by filtering the records where:&lt;br /&gt;
1) PostingEndDate has not passed today&apos;s date&lt;br /&gt;
OR&lt;br /&gt;
2) PostingEndDate is empty (blank value means the Job can be displayed to the user indefinitely)

Here is a cfdump (jobList_raw) of all records:
&lt;code&gt;
SELECT   JobID, DeptID, JobTitle, Salary, PostingEndDate
FROM     tblJob 
WHERE 	 ActiveJob = 1
ORDER BY JobTitle
&lt;/code&gt;
&lt;img src=&quot;/images/posts/1_rawdata.jpg&quot;&gt;
&lt;br /&gt;&lt;br /&gt;

&lt;strong&gt;Problem:&lt;/strong&gt; no easy way to write the WHERE clause. Here are some attempts:&lt;br /&gt;
1) Len(PostingEndDate) = 0&lt;br /&gt;
No error, but does not pick up those with blank dates:
&lt;code&gt;
SELECT   JobID, DeptID, JobTitle, Salary, PostingEndDate
FROM     tblJob 
WHERE 	 ActiveJob = 1
AND   (Now() &lt;= PostingEndDate OR Len(PostingEndDate) = 0)
ORDER BY JobTitle
&lt;/code&gt;

2) PostingEndDate = &apos;&apos;&lt;br /&gt;
Generates error: [Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression. &lt;br /&gt;
We can&apos;t compare the date field to an empty string.
&lt;code&gt;
SELECT   JobID, DeptID, JobTitle, Salary, PostingEndDate
FROM     tblJob 
WHERE 	 ActiveJob = 1
AND   (Now() &lt;= PostingEndDate OR PostingEndDate = &apos;&apos;)
ORDER BY JobTitle
&lt;/code&gt;

3) Cstr(PostingEndDate) = &apos;&apos;&lt;br /&gt;
Generates error: [Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] Invalid use of Null&lt;br /&gt; 
This was my attempt to Cast the date to a string using an Access function. Guess not.
&lt;code&gt;
SELECT   JobID, DeptID, JobTitle, Salary, PostingEndDate
FROM     tblJob 
WHERE 	 ActiveJob = 1
AND   (Now() &lt;= PostingEndDate OR Cstr(PostingEndDate) = &apos;&apos;)
ORDER BY JobTitle
&lt;/code&gt;
&lt;br /&gt;

&lt;strong&gt;Solution:&lt;/strong&gt; write a ColdFusion Query of Query (QoQ) to UNION the two conditions into one resultset. Here are the steps that lead to the final solution.&lt;br /&gt;
1) I still need a way to get a string representation (varchar) of the date field. So I added another column (PostingEndDate_str) to the Query object.
&lt;code&gt;
&lt;cfset QueryAddColumn(jobList_raw,&quot;PostingEndDate_str&quot;,&quot;varchar&quot;,Arraynew(1))&gt;
&lt;/code&gt;

2) Loop query and populate the new varchar field. This was intended to produce a blank string for what cfdump showed as [empty string]. Then I should be able to use: PostingEndDate = &apos;&apos; 
&lt;code&gt;
&lt;cfloop query=&quot;jobList_raw&quot;&gt;
	&lt;cfset QuerySetCell(jobList_raw, &quot;PostingEndDate_str&quot;, &quot;#PostingEndDate#&quot;,currentrow)&gt;
&lt;/cfloop&gt;
&lt;/code&gt;
NOPE! Still see [empty string] in the new field, THIS BECAME THE HAIR PULLING MOMENT OF THE SOLUTION AT THIS POINT, SO I STARTED GOOGLE SEARCHING.&lt;br /&gt;
&lt;img src=&quot;/images/posts/2_str_empty.jpg&quot;&gt;

I decided to try using &apos;-&apos; before and after the value, a trick I noticed in &lt;a href=&quot;http://www.bennadel.com/blog/379-ColdFusion-Query-of-Queries-Unexpected-Data-Type-Conversion.htm&quot;&gt;Ben Nadel&apos;s blog&lt;/a&gt;:
&lt;code&gt;
&lt;cfloop query=&quot;jobList_raw&quot;&gt;
	&lt;cfset QuerySetCell(jobList_raw, &quot;PostingEndDate_str&quot;, &quot;-#PostingEndDate#-&quot;,currentrow)&gt;
&lt;/cfloop&gt;
&lt;/code&gt;
Now I see &quot;--&quot; for all those [empty string] values, much better. I can work with that in the QoQ.&lt;br /&gt;
&lt;img src=&quot;/images/posts/3_str_dashes.jpg&quot;&gt;

3) Last is the Query of Query UNION.
&lt;code&gt;
SELECT	JobID, DeptID, JobTitle, Salary, PostingEndDate
FROM    jobList_raw 
WHERE   #ParseDateTime(DateFormat(Now(),&apos;mm/dd/yyyy&apos;))# &lt;= PostingEndDate  &lt;!--- Lefthand expression will format Now() as: 2010-03-19 00:00:00.0 ---&gt;
UNION
SELECT	JobID, DeptID, JobTitle, Salary, PostingEndDate
FROM    jobList_raw 
WHERE 	 PostingEndDate_str = &apos;--&apos;  &lt;!--- [empty string] dates will contain this value from the QuerySetCell loop executed above ---&gt;
ORDER BY JobTitle
&lt;/code&gt;

Here is the final recordset. It correctly leaves the Architect job filtered out because its PostingEndDate of Mar 9, 2010 has passed. The Mortgage Processor and Supervisor jobs with blank dates are kept in the results!&lt;br /&gt;
&lt;img src=&quot;/images/posts/4_no_architect.jpg&quot;&gt; 
				</description>
				
				<category>ColdFusion</category>				
				
				<category>SQL</category>				
				
				<pubDate>Sat, 20 Mar 2010 17:16:00 -0700</pubDate>
				<guid>http://blog.webdh.com/index.cfm/2010/3/20/Using-ColdFusion-with-a-stubborn-MS-Access-DateTime-field</guid>
				
			</item>
			
			<item>
				<title>SQL view error &quot;more column names specified than columns defined&quot;</title>
				<link>http://blog.webdh.com/index.cfm/2006/6/20/SQL-view-error-more-column-names-specified-than-columns-defined</link>
				<description>
				
				If you ever get a ColdFusion error message such as the following:

&lt;span style=&quot;font-family:courier&quot;&gt;
Error Executing Database Query. [Macromedia][SQLServer JDBC Driver][SQLServer]View or function &apos;dbo.myviewname&apos; has more column names specified than columns defined.
&lt;/span&gt;

You/somebody probably just added or deleted a column from the view definition. Then, if you have a cfquery that does a &quot;select *&quot; against the view, that error will result.

The fix is to run the following system stored procedure in SQL Query Analyzer, to refresh the view: &lt;b&gt;sp_refreshview &apos;myviewname&apos;&lt;/b&gt; 
				</description>
				
				<category>ColdFusion</category>				
				
				<category>SQL</category>				
				
				<pubDate>Tue, 20 Jun 2006 15:29:00 -0700</pubDate>
				<guid>http://blog.webdh.com/index.cfm/2006/6/20/SQL-view-error-more-column-names-specified-than-columns-defined</guid>
				
			</item>
			
			<item>
				<title>Doing a Subquery in SQL</title>
				<link>http://blog.webdh.com/index.cfm/2005/6/23/Doing-a-Subquery</link>
				<description>
				
				For my own future reference... 
This will lookup a value (emailaddress) from table2, to use in where clause in table1.
&lt;code&gt;
select *
from table1
where EmailAddress = (select EmailAddress from table2 where UserID = &apos;#URL.UID#&apos;)
&lt;/code&gt; 
				</description>
				
				<category>SQL</category>				
				
				<pubDate>Thu, 23 Jun 2005 10:00:00 -0700</pubDate>
				<guid>http://blog.webdh.com/index.cfm/2005/6/23/Doing-a-Subquery</guid>
				
			</item>
			</channel></rss>