Solved

Case Statement using Else to pull All values

Posted on 2015-01-06
9
53 Views
Last Modified: 2015-01-10
I want to use a Case statement to pull a row or rows based on a criteria.  If the criteria is not met I want to retrieve  all values for the field.  This is what i have so far...I am struggling on how to pull all rows in the else statement

Select * from payroll
WHERE
mgrid =
CASE
OFFICEID = 10 THEN  BOB101
ELSE
'I NEED TO PULL IN ALL ROWS
END
0
Comment
Question by:upobDaPlaya
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 10

Assisted Solution

by:HuaMinChen
HuaMinChen earned 100 total points
ID: 40534806
Try
Select * from payroll
WHERE
mgrid = 
CASE
OFFICEID = 10 THEN  BOB101
ELSE
mgrid
END

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40534856
Easier to use WHERE and precedence constraints in a WHERE clause than CASE

SELECT * FROM Payroll
WHERE (OFFICEID=10 AND mgrid='BOB101') OR (OFFICEID <> 10) 

Open in new window

0
 
LVL 14

Assisted Solution

by:Vikas Garg
Vikas Garg earned 100 total points
ID: 40534971
Hello Try this

Select * from payroll
WHERE

CASE WHEN 
OFFICEID = 10 THEN  CASE WHEN mgrid = 'BOB101' THEN 1 ELSE 0 END ELSE 1 END =1 

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40534974
I agree with Jim and would prefer to see you use his suggestion.

I'm just correcting the case expression syntax (of the first comment)

Select * from payroll
WHERE
mgrid =
CASE WHEN
      OFFICEID = 10 THEN  'BOB101' -- this appears to be a literal
ELSE
      mgrid
END
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:upobDaPlaya
ID: 40539357
Jim,

Can you explain your precedence comment ?
0
 
LVL 10

Expert Comment

by:HuaMinChen
ID: 40539363
You can try the way shown in above.
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 100 total points
ID: 40539432
WHERE
 (OFFICEID=10 AND mgrid='BOB101')  -- BOTH conditions are evaluated together
OR                                 -- or
(OFFICEID <> 10)                   -- this condition is considered

Open in new window

so,
if a record with OFFICEID=10 but mgrid <> 'BOB101' then it is excluded
any record with OFFICEID <> 10 is included
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 200 total points
ID: 40539453
Precedence constraints is the order of execution in an expression, and using parenthesis ( ) to override that order.

1 + (2 * 3) = 7
(1 + 2) * 3 = 9

It also applies to logical expressions like AND or OR, as PortletPaul demonstrates above.

(TRUE AND FALSE) OR TRUE = TRUE
TRUE AND (FALSE OR FALSE) = FALSE
0
 

Author Closing Comment

by:upobDaPlaya
ID: 40542638
Thanks..I used Jim's suggestion, but all of the input allowed me to evaluate and learn the CASE vs OR possibilities.. thanks !
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

743 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

16 Experts available now in Live!

Get 1:1 Help Now