Solved

Coldfusion SQL merge 2 columns

Posted on 2013-01-29
6
333 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
In this article I will describe the Backup & Restore 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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now