RSS 2.0 | Atom 1.0 | CDF

Search

Categories

Archive

Blogroll

Sign In

# Tuesday, April 17, 2007
Tuesday, April 17, 2007 2:43:30 PM (GMT Daylight Time, UTC+01:00) ( Database )
I found this out today, if you want a stored procedure to handle optional values without limiting the result set when the parameter is NULL, you can use a CASE/WHEN statement to ignore the parameter in the where clause if it is null.  you just set ColumnX=ColumnX via the CASE statement, which will always be true, effectively resulting in (TRUE AND {remaining condition})
Procedure dbo.Select_Totals_For_Year
(
@Name NvarChar(200),
@Year int
)
AS
BEGIN
select Name, Count(ID) AS [Total]
FROM Table1
WHERE Name = @Name AND
Year = (CASE WHEN @Year is null THEN Year ELSE @Year END)
group by Name
order by Name
END
Comments [5] | | # 
Tuesday, April 24, 2007 12:31:00 AM (GMT Daylight Time, UTC+01:00)
Very nice.

Now if there was only a way to allow null values and operate on some other flag value for the column.
Glenn
Wednesday, May 30, 2007 7:23:01 AM (GMT Daylight Time, UTC+01:00)
It works brilliantly, thanks
Mduduzi
Tuesday, June 30, 2009 9:55:50 PM (GMT Daylight Time, UTC+01:00)
Tim, I've been pulling my hair out for the last 2 hours trying to figure something like this out. Thanks!
Greg
Wednesday, March 03, 2010 2:19:29 PM (GMT Standard Time, UTC+00:00)
Good work, Thanks
Sampath
Tuesday, May 04, 2010 12:33:34 PM (GMT Daylight Time, UTC+01:00)
that's great, thanks.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview