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 9004193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:50:50+00:00 2026-06-16T00:50:50+00:00

I am working on a select statement in SQL and am running into issues

  • 0

I am working on a select statement in SQL and am running into issues trying to create a where clause that includes a case statement or an if else statement. I want to select records based on the value of a variable. If the variable is ‘True’ then only return records from the select statement where a column is null. If the variable is not ‘True’ then return all records regardless if that columns is null.

Any tips on how to do this?

Below is a simple example of what i am trying to do:

declare @option1 as varchar(5)

--This can be True or False so to test i just put the Set option below
set @option1 = 'True'

Select a,b,c,d...
from ...
where d = case when @option1 = 'True' then NULL End

This is the part where i do not know what to do. I only need to filter out the records if the variable is ‘True’ so not sure what to put in the else section of the case.

  • 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-16T00:50:51+00:00Added an answer on June 16, 2026 at 12:50 am

    You can’t test for d = NULL as your CASE statement does because that will always return false since NULL is not equal to NULL (unless you set ANSI_NULLS to ‘off’).

    The simplest thing to do would be to change the WHERE clause to this:

    WHERE @option1 = 'False' OR d IS NULL
    

    If you prefer to use a CASE statement for some reason, you can write it like this:

    WHERE 1 = CASE WHEN @option1 = 'False' THEN 1 
                   WHEN @option1 = 'True' AND d IS NULL THEN 1 
                   ELSE 0 
              END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some working code that uses a SQL select statement within it. As
I have a perplexing SQL select statement (ugly) that I'm trying to write in
Working in MS SQL 2005 and I want to use a select statement within
I have a sql function that does a simple sql select statement: CREATE OR
I'm unable to convert this SQL query into a working linq statement select sum(cena),
I'm trying to write a simple winforms application that executes a SQL SELECT statement
I am trying to create an SQL statement that will essentially calculate points due
I am working on my SQL, and I have the following statement: 1 SELECT
I've been working on a stored procedure that runs a select statement in a
I have this working SQL statement: select oriseqs.newID from oriseqs WHERE oriseqs.singlets=1 AND oriseqs.newID

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.