SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,165 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I have a sql statement something like below but I want the AND condition to be executed only when the @param1 is not NULL, else I want all the record to be retuned for ID 123. Can you please advise on how to accomplish this task?
Declare @param1 varchar(255) = NULL
SET @param1='B1, B2'
SELECT C.* FROM Customer C
WHERE C.ID = 123
AND C.Degree IN ISNULL(@param1, C.Degree)
This will work in certain circumstances:
SELECT *
FROM Customer
WHERE ID = 123
AND ( @param1 is null or Degree IN (select trim(value) from string_split(@param1, ',')) )
option (recompile)