Happy
Happy

Reputation: 323

How to check parameter is not null value sql server

How to check parameter has null or not in stored procedure

e.g

select * from tb_name where name=@name

i need to check if @name has values or null means.how to do it.thanks...

Upvotes: 0

Views: 84

Answers (3)

Ryan Schlueter
Ryan Schlueter

Reputation: 2221

select * 
from tb_name 
where ((@name  is null) or ([name] = @name))

Upvotes: 1

Avi Turner
Avi Turner

Reputation: 10456

In case you want results where Name is not null and equal to @name Try:

select * from tb_name where name=@name AND @name IS NOT NULL

If you want results where Name is null Or equal to @name Try:

select * from tb_name where name=@name OR @name IS NULL

Where you looking for one of those?

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1271231

Is this what you want?

select * from tb_name where name=@name and @name is not null

Actually, the extra check is unnecessary, because NULL will fail any comparison. Sometimes, NULL is used to mean "get all of them". In that case, you want:

select * from tb_name where name=@name or @name is null

Upvotes: 2

Related Questions