Avatar of conrad2010
conrad2010
 asked on

T-SQL null values in parameters

this is a simplified query where I am passing a value to @sAlphaGroup but sometimes that value is null and I need to deal with this in one line...

this doesn't work (no records returned where in this case I need all records):

declare @sAlphaGroup varchar(20)
set @sAlphaGroup = null

Select
      *
From dbo.Projects As P
     Join dbo.tblProjectProducts As Product
          On Product.ProjectId = P.ProjectId
Where
      P.IsActive = 1
     And Product.IsActive = 1
     And Product.IsOnWebSite = 1
     AND (@sAlphaGroup IS NOT NULL OR PP.ProductTitle LIKE '[' + @sAlphaGroup + ']%')
Order By
    P.Title      

Microsoft SQL Server 2005SQL

Avatar of undefined
Last Comment
conrad2010

8/22/2022 - Mon
Lowfatspread

IS NOT NULL
ASKER CERTIFIED SOLUTION
Lowfatspread

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
jorgedeoliveiraborges

You typo 'not null'. You need to type 'is not null'
  AND (@sAlphaGroup IS NOT NULL OR PP.ProductTitle LIKE '[' + @sAlphaGroup + ']%')

Open in new window

conrad2010

ASKER
this worked!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck