Solved

Select case statement in OPENQUERY statement

Posted on 2010-09-23
7
1,410 Views
Last Modified: 2012-05-10
The following is my code and works up till the second to last line or this one

Case when ELIG_NPS_BONUS IS NULL then BONUS_CNTL_NBR IS NULL else BONUS_CNTL_NBR end NPS_BONUS_CNTL_NBR

What I am trying to do is when  ELIG_NPS_BONUS IS NULL then another field BONUS_CNTL_NBR IS NULL  else show me what in the BONUS_CNTL_NBR

Is there a way to do this, it is an openquery that is hitting an Oracle server and returning the data.
Select * from OPENQUERY(SIDPERS, 'Select ACN, APP_Name, ADMIN_REMARKS, substr(SSN_SM,6,9), UIC, PARA, LINE, 

	GRADE, MOS, DT_EXPIRE, NCOID, 

	Case when ELIG_SLRP IS NULL then NULL else ''X'' end ELIG_SLRP,

	Case when SLRP_CNTL_NBR IS NULL then NULL else SLRP_CNTL_NBR end SLRP_CNTL_NBR,

	Case when ELIG_NPS_BONUS IS NULL then NULL  else ''X'' end ELIG_NPS_BONUS,

	Case when ELIG_NPS_BONUS IS NULL then BONUS_CNTL_NBR IS NULL else BONUS_CNTL_NBR end NPS_BONUS_CNTL_NBR

	from ACN.ACN_CNTL_NBR_TBL where ACN = ''09-119-003''')

Open in new window

0
Comment
Question by:kdeutsch
  • 3
  • 2
  • 2
7 Comments
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 125 total points
Comment Utility
Case when ELIG_NPS_BONUS IS NULL and BONUS_CNTL_NBR IS NULL then null else BONUS_CNTL_NBR end NPS_BONUS_CNTL_NBR
0
 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
Select * from OPENQUERY(SIDPERS, 'Select ACN, APP_Name, ADMIN_REMARKS, substr(SSN_SM,6,9), UIC, PARA, LINE,
      GRADE, MOS, DT_EXPIRE, NCOID,
      Case when ELIG_SLRP IS NULL then NULL else ''X'' end ELIG_SLRP,
      Case when SLRP_CNTL_NBR IS NULL then NULL else SLRP_CNTL_NBR end SLRP_CNTL_NBR,
      Case when ELIG_NPS_BONUS IS NULL then NULL  else ''X'' end ELIG_NPS_BONUS,
      Case when ELIG_NPS_BONUS IS NULL then NULL else BONUS_CNTL_NBR end NPS_BONUS_CNTL_NBR
      from ACN.ACN_CNTL_NBR_TBL where ACN = ''09-119-003''')
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 225 total points
Comment Utility
[Case when ELIG_NPS_BONUS IS NULL and BONUS_CNTL_NBR IS NULL then null else BONUS_CNTL_NBR end NPS_BONUS_CNTL_NBR]

is a long way to write just

BONUS_CNTL_NBR

because the CASE is only true when BONUS_CNTL_NBR is null, in which case.. it is already null... otherwise, show the value of BONUS_CNTL_NBR.
0
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.

 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
You're right - I didn't even realize they were the same column.  (cut-n-paste)
0
 

Author Comment

by:kdeutsch
Comment Utility
all

Ok, see the error of my ways, what about this, I just noticed I made another mistake in that I need to check for a zero or a null

Case when ELIG_SLRP = ''0'' or when ELIG_SLRP is NULL then NULL else ''X'' end ELIG_SLRP,
0
 
LVL 58

Expert Comment

by:cyberkiwi
Comment Utility
Case when ELIG_SLRP = '0' or ELIG_SLRP is NULL then NULL else 'X' end ELIG_SLRP,

Since you are quoting the zero I assume it is varchar?
You could also do this

Case when nvl(ELIG_SLRP,'0') = '0' then NULL else 'X' end ELIG_SLRP,
0
 

Author Closing Comment

by:kdeutsch
Comment Utility
thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

771 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

11 Experts available now in Live!

Get 1:1 Help Now