?
Solved

Select case statement in OPENQUERY statement

Posted on 2010-09-23
7
Medium Priority
?
1,536 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
[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
  • 2
7 Comments
 
LVL 33

Assisted Solution

by:knightEknight
knightEknight earned 500 total points
ID: 33748095
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
ID: 33748132
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 900 total points
ID: 33748155
[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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 33

Expert Comment

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

Author Comment

by:kdeutsch
ID: 33748385
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
ID: 33748448
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
ID: 33748461
thanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

752 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