Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:32:52+00:00 2026-06-15T09:32:52+00:00

I want to use case statement in where clause but getting error in the

  • 0

I want to use case statement in where clause but getting error in the query below.

where 
 (r.WeekId=@WeekId or @WeekId is null) and
 (ShiftId=@ShiftId or @ShiftId is null)
 and (placeid=@PlaceId or @PlaceId is null)
 and (r.StatusId=3)
 AND     
CASE WHEN @day = 1 THEN ((firstday=@time and (allocateddays is null or NOT (AllocatedDays  LIKE '%1%')))) 
 WHEN @day = 2 THEN ((secondday=@time and (allocateddays is null or NOT (AllocatedDays LIKE '%2%'))))  
 WHEN @day = 3 THEN ((thirdday=@time  and (allocateddays is null or NOT (AllocatedDays LIKE '%3%')))) 
 ELSE AND (1 = 1) 
END

I am getting error at Case statement at line CASE WHEN @day = 1. What is going wrong with my query. Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:32:54+00:00Added an answer on June 15, 2026 at 9:32 am

    The CASE should be rewritten as an OR chain each of which uses @day = n for one of its conditions, owing to the fact that it CASE is expceted to return a value. It cannot execute conditionally in a WHERE clause.

    If the condition like @day = 1 returns TRUE, it is used as a boolean AND with the rest of the statement. Each of these is chained via OR to the previous.

    where 
     (r.WeekId=@WeekId or @WeekId is null) and
     (ShiftId=@ShiftId or @ShiftId is null)
     and (placeid=@PlaceId or @PlaceId is null)
     and (r.StatusId=3)
     /* I *think* I have the () groups correct here... There's an unnecessary double (()) around firstday|secondday|thirddad */
     AND (
       (@day = 1 AND ((firstday=@time and (allocateddays is null or NOT (AllocatedDays  LIKE '%1%')))))
       OR (@day = 2 AND ((secondday=@time and (allocateddays is null or NOT (AllocatedDays LIKE '%2%')))))
       OR (@day = 3 AND ((thirdday=@time  and (allocateddays is null or NOT (AllocatedDays LIKE '%3%')))))
       /* Not certain if this is necessary in your case, as a catch-all to replace your ELSE */
       OR (@day NOT IN (1,2,3) AND 1=1) 
     )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C#.Net and MYSQL I want to use a CASE Statement like SQL Query
I want my sql statement to use order by clause using Case Statement and
I want to use a case statement where my logic determines a date. If
I want to use the pl/sql case when statement like in java or c++.
I want to use CASE in my stored procedure. I am getting some syntax
I want to use a clause along the lines of CASE WHEN ... THEN
I want to use a case statement in Pervasive as it lacks support for
I want to use a CASE Statement to check a parameter conditionally like if
I want to use a case statement in my user-defined functions because I need
i want to use case condition for getting values between dates. i have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.