Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Coldfusion SQL merge 2 columns

Posted on 2013-01-29
6
Medium Priority
?
350 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
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 

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 2000 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

705 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