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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:43:58+00:00 2026-05-31T15:43:58+00:00

I have a table that contains a varchar column that allows null or empty

  • 0

I have a table that contains a varchar column that allows null or empty values.

I’m writing a stored procedure that contains a variable that can be assigned to a null value or a regular string (not empty)

It’s defined like this:

declare @myvar varchar(50)

Now I’m trying to write a query that returns the rows where the column is equal to this variable or it is empty or null. So far I thought this was going to work:

select * from mytable where mycolumn =@myvar or mycolumn =''

However if the column is null and the variable is null it will not return any values.

I know I can make it work doing something like this:

select * from mytable where (mycolumn = @myvar and mycolum is not null) or mycolumn is null or mycolumn =''

Is there a better way of doing this?

PS: I would prefer not to do ANSI_NULLS OFF

Thanks

  • 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-05-31T15:43:59+00:00Added an answer on May 31, 2026 at 3:43 pm

    You don’t need this part: and mycolum is not null since, as you said, if your variable is NULL it still won’t match. So I think that this is actually the best way:

    select * 
    from mytable
    where mycolumn = @myvar or mycolumn is null or mycolumn =''
    

    If you want to do the same, but with less conditions, you could do:

    select * 
    from mytable
    where mycolumn = @myvar or ISNULL(mycolumn,'') = ''
    

    but this will not use an index (if there is one) on mycolumn.

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

Sidebar

Related Questions

I have a varchar(100) column in a table that contains a mix of integers
I have a table that contains a Date column in varchar format. The date
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a table that contains log entries for a program I'm writing. I'm
I have a table that contains the next columns: ip(varchar 255), index(bigint 20), time(timestamp)
I have a table T (structure below) which initially contains all-NULL values in an
I have a table that has, among its primary keys, a VARCHAR(16) column that
I have a table that contains a column that is XML data type. I
I have a database column in a SQL Server 2008 database that contains VARCHAR
I have a database in Ingres 9.2.0 that contains a long varchar column. Is

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.