Link to home
Start Free TrialLog in
Avatar of NursingCorp
NursingCorpFlag for United States of America

asked on

Assistance with stored procedure setting a blank variable to retrieve all

I have a stored procedure with variables being sent to it such as dates, facility, employees, which are used in the final SELECT statement (See snippet). Sometimes an Employee or Facility is not given, because we want to see all employees at a given Facility.

I have tried setting the value of the variable @Employee = '%' but it will not return all. How can I get it to show all if the value of this variable is left blank?

Thanks
ALTER   PROC [dbo].[spNC_BookingWizard]
	@StartDate varchar(10) = '1/1/1900',
	@EndDate varchar(10) = '1/1/2010',
	@Employee varchar(200) = '',
	@Facility varchar(200) = '',
	@Status varchar(200) = '',
	@test bit = 0
AS
 
SET NOCOUNT ON
 
DECLARE @dtStart datetime
DECLARE @dtEnd datetime
 
 
SET @dtStart = CONVERT(datetime, @StartDate)
SET @dtEnd = DATEADD(dd, 1, DATEADD(dd, 0, CONVERT(datetime, @EndDate)))
 
...........
 
SELECT * FROM #tmpBook 
WHERE StartDate_date >= @dtStart AND StartDate_date <= @dtEnd 
AND Facility = @Facility
AND EmpName = @Employee
AND  BookingStatus = @Status

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

if the column value is NULL, you would have to make the condition WHERE yourcol IS NULL.

for you, this syntax should work:
SELECT * FROM #tmpBook 
WHERE StartDate_date >= @dtStart AND StartDate_date <= @dtEnd 
AND ( Facility = @Facility OR @Facility = '%' )
AND ( EmpName = @Employee OR @Employee = '%' )
AND  BookingStatus = @Status

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of BrandonGalderisi
BrandonGalderisi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NursingCorp

ASKER

Copied and pasted...worked perfect. Thanks