Groups | Blog | Home
all groups > sql server (alternate) > march 2004 >

sql server (alternate) : Choose WHERE "(x IS NULL)" or "(x IS NOT NULL)" based on parameter?


HumanJHawkins
3/26/2004 9:03:53 PM
Hi,

I want to make a stored procedure that returns either the NULL results, or
the NOT NULL results from table comparison based on a parameter passed into
it. For example, something like this:

WHERE ((MyData IS NULL) = @ShowNulls)

In other words, if the @ShowNulls parameter is true, show the nulls.
Otherwise, show the NOT NULLS. The syntax above doesn't work... Any
suggestions?

Thanks!

David Portas
3/26/2004 9:44:47 PM
....
WHERE
(x IS NULL AND @shownulls = 'Y')
OR (x IS NOT NULL AND @shownulls = 'N')

There isn't a boolean datatype in SQL and you can't compare a logical
expression to a variable.

--
David Portas
SQL Server MVP
--

HumanJHawkins
3/26/2004 10:05:33 PM
[quoted text, click to view]
<CUT>

Thanks! Boy do I feel silly, now that I see the solution!

Hugo Kornelis
3/26/2004 10:45:57 PM
[quoted text, click to view]

SQL Server has no boolean datatype, so you won't be able to pass a
parameter as "true" or "false". But apart from that detail, what you
want should be possible:

CREATE PROC MyProc
@ShowNulls CHAR(5)
AS
SELECT Whatever, Column, You, Want
FROM YourTable
WHERE (MyData IS NULL AND @ShowNulls = "True")
OR (MyData IS NOT NULL AND @ShowNulls = "False")
GO

(untested)

Best, Hugo
--

AddThis Social Bookmark Button