Solved

Coldfusion SQL merge 2 columns

Posted on 2013-01-29
6
332 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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 …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

14 Experts available now in Live!

Get 1:1 Help Now