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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:58:57+00:00 2026-05-29T05:58:57+00:00

I have a query that I’m building for an application. The database is setup

  • 0

I have a query that I’m building for an application. The database is setup in SQL Server 2008. I want to use a query similar to below, however, I will be using this ‘Where’ clause for about 4 other columns using the same requirements. Is this the appropriate way to test for null or ” in a column that is VarChar(255) and does allow nulls.

Ideally, if the variable @UutSerialNumber is null or empty (”), I want all the results, but if it is not, I want to use the ‘LIKE’ clause. Is this the proper way of doing this and will it work? It seems to work until I start adding more columns to the Where clause.

Also, how would I handle a “text” datatype using the same type of query?

SELECT DeviceName, UutStatus
FROM MyTable
WHERE (UutSerialNumber LIKE '%' + @UutSerialNumber + '%' OR UutSerialNumber LIKE '%%' AND (@UutSerialNumber = '' OR @UutSerialNumber IS NULL)) AND ...

Help is appreciated. Thanks everyone!

  • 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-29T05:58:58+00:00Added an answer on May 29, 2026 at 5:58 am

    It amy seem like duplication of SQL but the best way to do this is in terms of performace is using IF … ELSE

    IF ISNULL(@UutSerialNumber, '') = '' 
        BEGIN
            SELECT  DeviceName, UutStatus 
            FROM    MyTable 
            -- MORE EXPRESSIONS
        END
    ELSE 
        BEGIN
            SELECT  DeviceName, UutStatus 
            FROM    MyTable 
            WHERE   (UutSerialNumber LIKE '%' + @UutSerialNumber + '%' 
            -- MORE EXPRESSIONS
        END
    

    It can be done within the WHERE clause if you are doing it on multiple columns and the query you posted wasn’t far off it was just missing additional parenthesis along with having a redundant clause.

    SELECT  DeviceName, UutStatus 
    FROM    MyTable 
    WHERE   (ISNULL(@UutSerialNumber, '') = '' OR UutSerialNumber LIKE '%' + @UutSerialNumber + '%')
    AND     (ISNULL(@AnotherParameter, '') = '' OR AnotherColumn LIKE '%' + @AnotherParameter + '%')
    

    Convert the text type to VARCHAR(MAX).

    as a footnote, I personally would use the CHARINDEX rather than concatenating strings in the like:

    WHERE   (ISNULL(@UutSerialNumber, '') = '' OR CHARINDEX(@UutSerialNumber, UutSerialNumber) > 0)
    

    This is nothing more than a footnote however as I have done no performance testing, I just think it is easier on the eye!

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

Sidebar

Related Questions

I have a query that I'm executing from a .NET application to a SQL
I have a query that runs super fast when executed in the sql editor
I have a query that I always want to return 10 records for: set
I have this query that i should make it run on SQL Azure. WITH
I have this query that retreives a list of id's + NAME: $sql =
I have a query that pulls values from multiple tables. I want to order
I have query like that in my DataSet (database is located in .mdf file):
I have a query that counts member's wedding dates in the database. SELECT SUM(NumberOfBrides)
I have a query that uses a TOP 1 clause (query2) that I want
i have query that maybe haven't any element on sequence and i want to

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.