0

I have a simple stored procedure that looks something like this:

SET @email = NULL

SELECT * FROM empTable
WHERE email = ISNULL(@email, '')

@email will take input from the user. The problem is, the email column in the table can be an empty string or null (or an email).

If the user doesn't enter any value, it should return all Employees with either NULL or empty string email.

I have tried to following but it didn't work too:

SELECT * 
FROM empTable
WHERE email = ISNULL(@email, '') OR email = ISNULL(@email, NULL)

3 Answers 3

1
SELECT * FROM empTable
WHERE email = ISNULL(@email, '') OR (email IS NULL AND @email IS NULL)

Or:

SELECT * FROM empTable
WHERE ISNULL(email,'') = ISNULL(@email, '') 
Sign up to request clarification or add additional context in comments.

Comments

0

if email is then condition after second OR will be considered

if email is present then first curly brackets will be considered

SELECT * FROM empTable
WHERE (email=@email) or (@email is null OR email = ISNULL(@email, ''))

3 Comments

It doesn't work, now if the input is NULL, it will just return EVERYTHING
If the user doesn't enter any value, it should return all Employees
it should return "all Employees with either NULL or empty string email"
0

Try like this:

IF @email has value then corresponding row will retrieve from DB; else second condition will execute which will retrieve email with NULL and empty string.

SELECT * 
FROM empTable
WHERE email = ISNULL(@email, '') OR (email IS NULL and email = '')

1 Comment

Thanks for upvote+downvote. Hope you got the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.