Solved

Coldfusion SQL merge 2 columns

Posted on 2013-01-29
6
346 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Copy Database Wizard 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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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