Groups | Blog | Home
all groups > visual studio .net general > june 2003 >

visual studio .net general : SQL Procedure


Bob Twickler
6/30/2003 6:34:05 PM
I am trying to run a SQL procedure that will return either a bit = true or
bit = true, false or NULL.

Does anybody know the WHERE Statement that will return any values including
possible NULL values.

What I want is if "@Saved" = 1 (true),,, return only Saved = 1;
if @Saved = 0 (false),,,, return all records.
In other words retrun all records (including NULLS) if @Saved is 0 (False)

I have tried

Where Saved = CASE @Saved = 1 THEN 1 ELSE ?????? END

What would ??????? be to return all records.

I hope someone will show me how simple this is.

Bob

John V. Barone
6/30/2003 10:11:19 PM
Try
--assume bit varible @Saved
SELECT blah
FROM blah
WHERE ISNULL(MyBitColumnName, 0) = @Saved

HTH,
John Barone


[quoted text, click to view]

John V. Barone
6/30/2003 10:19:28 PM
Sorry, I misread your original note in my first reply, which will return =
only 0 or Nulls (and not all records), if @Saved =3D 0.

Instead, try:
--Assume a bit variable @Saved
SELECT blah
FROM blah
WHERE MyBitColumnName =3D CASE @Saved =20
WHEN 1 THEN 1=20
ELSE MyBitColumnName=20
END

Basically, what the above does is...
1. If @Saved =3D 1, compare column value to 1 (no problem)
2. Otherwise, compare the column value to the column value. Since a =
column values always equals itself (even if Null), all the records will =
be returned. (BTW, this is trick I use to allow multiple parameter =
searches in a single sproc.)

Hth,
John Barone



[quoted text, click to view]
John V. Barone
6/30/2003 10:34:44 PM
Sorry all, I should have been more careful before posting. The solution =
requires two elements, and I captured each one of the them in each of my =
previous posts, but not both together...
This **will** work (promise, scouts' honor):
--Assume a bit variable @Saved
SELECT blah
FROM blah
WHERE ISNULL(MyBitColumnName, 0) =3D CASE @Saved
WHEN 1 THEN 1=20
ELSE ISNULL(MyBitColumnName, 0)
END

Basically, what the above does is...
1. If @Saved =3D 1, compare column value to 1 (no problem)
2. Otherwise, compare the column value to the column value. Since a =
column's value always equals itself (except when Null), comparing the =
MyBitColumnColumn to itself will take care of the 1s and 0s. Using =
ISNULL takes care of the Nulls.

Hth,
John Barone
[quoted text, click to view]
Sorry, I misread your original note in my first reply, which will =
return only 0 or Nulls (and not all records), if @Saved =3D 0.

Instead, try:
--Assume a bit variable @Saved
SELECT blah
FROM blah
WHERE MyBitColumnName =3D CASE @Saved =20
WHEN 1 THEN 1=20
ELSE MyBitColumnName=20
END

Basically, what the above does is...
1. If @Saved =3D 1, compare column value to 1 (no problem)
2. Otherwise, compare the column value to the column value. Since a =
column values always equals itself (even if Null), all the records will =
be returned. (BTW, this is trick I use to allow multiple parameter =
searches in a single sproc.)

Hth,
John Barone



[quoted text, click to view]
AddThis Social Bookmark Button