Solved

SQL Query Help

Posted on 2014-04-01
3
270 Views
Last Modified: 2014-04-01
I have a really massive SQL query that I use to run an export from one CRM into another CRM package.  I just uncovered a problem with my query and I am not sure how to fix it.  My basic syntax is this:

With C1 As
(
Select statement
),
C2 As
(
Select statement
)
select c1.value, C2.value
from C1
left join c2

I have 17 different joins here because this query is huge. Here is my question.  One of the select queries in the top part of the statement is returning a null value sometimes.  When it returns a null I need to run a different query to produce the correct values.  I always have trouble creating programming logic in SQL but I basically want:

if query = NULL then query2.

Can anyone make any sense of this and tell me how I can accomplish this?  I am running in MS SQL 2008.

Thank you!
0
Comment
Question by:mattpayne59
3 Comments
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
You need to use ISNULL and CASE in your statements.
0
 

Author Comment

by:mattpayne59
Comment Utility
Ok but how do I structure it because I dont know if I need the other case until after I complete the select query?
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
lochan is correct. In order to plugin the ISNULL/COALESCE or CASE, you have to provide the whole query.
Or try embedding like this.
With C1 As
(
Select statement
),
C2 As
(
Select statement
)
select COALESCE(c1.value,C2.value) as Value
from C1
left join c2

Open in new window

0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now