开发者

How to return both null and not-null values in one query

开发者 https://www.devze.com 2023-03-21 22:04 出处:网络
I have an integer field person_id with the default value of NULL. I am using a stored procedure and pulling people names and address. if a user do开发者_JAVA百科es not pass a value all records are pul

I have an integer field person_id with the default value of NULL. I am using a stored procedure and pulling people names and address. if a user do开发者_JAVA百科es not pass a value all records are pulled both, null and not nulls. How do I do it. May be Case statement or otherwise?

my_procedure(          <--- this is pseudo code
@person_id int null
begin
select name, address from PERSON
WHERE person_id like case NULL then NULL else '%'
end

Null requires -> IS NULL number requires = 123

EDIT I need a condition too, if nothing is passed, all values are pulled. If something is passed, that value is searched


WHERE (@personID is NULL OR personid IS NULL OR personid = @personid)

Just put both your cases in a parenthetical WHERE clause.

EDIT

Added another OR clause to cover what I THINK you are asking for.


The correct code in this case is

IsNULL(mycolum,'') LIKE CASE when person_id is null then '%' else '%' + person_id + '%' 

IsNULL coverts null values into empty string and then you can worry about only strings, not null values.

0

精彩评论

暂无评论...
验证码 换一张
取 消