?
Solved

Case Statement using Else to pull All values

Posted on 2015-01-06
9
Medium Priority
?
65 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

807 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