0

I always endup doing this to cover all situations where @variable can be empty or null,

 IF(COALESCE(@variable,'') <> '')
    BEGIN


    END 

Is this the most optimal way to check this?

2 Answers 2

4

If its a variable, then it shouldn't matter. If you are doing a similar filter on a column of a table, then I would recommend: WHERE Column IS NOT NULL OR Column <> '' as it mantains the use of a possible index on that column.

Sign up to request clarification or add additional context in comments.

2 Comments

what you mean by it does not matter on a variable?
He means there is no performance impact for variables, when compared to separating the IS NULL test from the <> '' test, because variables aren't indexed.
1

Assuming @variable is a VARCHAR/NVARCHAR then the following should do the same job:

IF @variable > ''
    BEGIN
    ...    
    END  

Comments

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.