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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:21:34+00:00 2026-06-05T21:21:34+00:00

I am having some difficulty returning all the results I would expect when leaving

  • 0

I am having some difficulty returning all the results I would expect when leaving an optional sql parameter blank on a column that contain null values.

Imagine you have a table with the following (referredby is optional and can therefore be NULL):

Customertable
ID    CustomerName  ReferredBy
1      Aaron         Joe
2      Peter         NULL
3      Steven        Joe

Suppose I want to query with an optional SQL parameter for the referredby field like such:

declare @referredby as varchar(15)

select id, customername
from customertable<br>
where referredby = isnull(@referredby, referredby)

If I leave the parameter null, this would only return:
1 Aaron
3 Steven

How can I use an optional parameter to return all 3 results?

  • 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-05T21:21:35+00:00Added an answer on June 5, 2026 at 9:21 pm

    Try This:

    select id, customername
    from customertable
    where (referredby = @referredby OR @referredby is null)
    

    For reasons explained in this post, comparing null = null in sql server returns false (or unknown). As does null != null.

    If you really like your syntax I believe you could make it work by setting ansi_Nulls to off: set ansi_nulls off

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that
I'm having some difficulty with my Entity Framework context that is proving very troublesome
I am having some difficulty locating information of comparing C strings. I understand that
Having some difficulty with what I thought would be straight forward. I am trying
I am having some difficulty writing a function that will search through a directory
I'm having some some difficulty predicting how my C code will truncate results. Refer
I am having some difficulty compiling a C++ program that I've written. This program
i´m having some difficulty applying style override to all widgets in my wordpress theme.
I'm having some difficulty with backbone js. Currently, I have a rails app that
im having some difficulty trying to pull out a specific value from a cookie.

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.