?
Solved

Case Statement using Else to pull All values

Posted on 2015-01-06
9
Medium Priority
?
60 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
[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
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 11

Assisted Solution

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

Open in new window

0
 
LVL 66

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 15

Assisted Solution

by:Vikas Garg
Vikas Garg earned 400 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 49

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
 

Author Comment

by:upobDaPlaya
ID: 40539357
Jim,

Can you explain your precedence comment ?
0
 
LVL 11

Expert Comment

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

Assisted Solution

by:PortletPaul
PortletPaul earned 400 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 66

Accepted Solution

by:
Jim Horn earned 800 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

764 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