Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Return all results (including null) when SQL stored procedure parameter is null

I have SQL table that contains null values for ex:

ID NAME CODE
1 Joe 123
2 Doe 456
3 Soe NULL

I want to check if the where clause parameter is null then return all table, what I tried:

@devCode int = NULL

SELECT * FROM myTable WHERE CODE = ISNULL(@devCode, CODE)

But the last row (ID 3) gets excluded when @devCode = NULL. I want that to be included too in result when the parameter is null.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

p.s. I also tried IIF and CASE but the result is the same.

>Solution :

I think it will work

SELECT * FROM myTable WHERE CODE = @devCode OR @devCode IS Null
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading