Solved

Coldfusion SQL merge 2 columns

Posted on 2013-01-29
6
337 Views
Last Modified: 2013-01-30
I am running Coldfusion 9 and SQL 2008 and am trying to write a query to merge two date columns (openDate and pastDate) from the same table into one column and extract the year from it so I can sort by year.
I have:

<cfquery name="getPast" datasource="Exhibitions">
           select Year(openDate + pastDate) as exhibitYear
            from dbo.exhibitionsnew
           order by exhibitYear desc
</cfquery>

but this just gives me an empty string for all records.
Is there a way to do this?
Thank you.
0
Comment
Question by:cbeverly
  • 3
  • 2
6 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 38832854
Can you give me some example data for openDate and pastDate?  If they are DateTime records, the + operator is not appropriate.  e.g. what does Jan 29, 2013 + Dec 13, 2012 mean?
0
 

Author Comment

by:cbeverly
ID: 38832868
OK I guess I am using the wrong syntax then. I don't want to combine the columns, I want to be able to group both columns into one, extract the year so I can sort by year
So if I have Jan 29, 2012 in openDate and March 30, 2013 in pastDate I want to extract the year from both columns and have a report that sorts by year so:

2012
  Jan 29, 2012

2013
 March 20, 2013
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38832954
A UNION (or UNION ALL) would let you merge the values into a single column. Something like this:

*Note:  UNION only returns distinct values. UNION ALL returns everything

   SELECT  Year(openDate) AS exhibitYear
   FROM    dbo.exhibitionsnew
   UNION  
   SELECT  Year(pastDate) AS exhibitYear
   FROM    dbo.exhibitionsnew
   ORDER BY exhibitYear DESC
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:cbeverly
ID: 38835236
This worked as long as I only had the one field, but when I added the rest back in, I got an error message because one of the fields - Headline - was a text field and I got this error message:  [Macromedia][SQLServer JDBC Driver][SQLServer]The text data type cannot be selected as DISTINCT because it is not comparable.
 .
SELECT  Year(openDate) AS exhibitYear, webDirectory, Headline
                           FROM    dbo.exhibitionsnew
                           UNION  
                           SELECT  Year(pastDate) AS exhibitYear, webDirectory, Headline
                           FROM    dbo.exhibitionsnew
                           ORDER BY exhibitYear DESC
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 38835366
Oh .. you didn't mention having a "text" column. Well like the message says "text" column can't be used with UNION or DISTINCT.   EDITNot unless you CAST it to "varchar" (which would obviously mean truncating the result).  But ... I'm not sure UNION makes sense if you're including other fields.  

> *Note:  UNION only returns distinct values. UNION ALL returns everything

Did you try using UNION ALL?
0
 

Author Comment

by:cbeverly
ID: 38835465
UNION ALL worked. Thanks so much!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question