Null or empty check for a string variable

158,459

Solution 1

Yes, that code does exactly that.

You can also use:

if (@value is null or @value = '')

Edit:

With the added information that @value is an int value, you need instead:

if (@value is null)

An int value can never contain the value ''.

Solution 2

Use This way is Better

if LEN(ISNULL(@Value,''))=0              

This check the field is empty or NULL

Solution 3

Yes, you could also use COALESCE(@value,'')='' which is based on the ANSI SQL standard:

SELECT CASE WHEN COALESCE(@value,'')='' 
    THEN 'Yes, it is null or empty' ELSE 'No, not null or empty' 
    END AS IsNullOrEmpty

DEMO

Solution 4

Yes, it works. Check the below example. Assuming @value is not int

WITH CTE 
AS
(
    SELECT NULL AS test
    UNION
    SELECT '' AS test
    UNION
    SELECT '123' AS test
)

SELECT 
    CASE WHEN isnull(test,'')='' THEN 'empty' ELSE test END AS IS_EMPTY 
FROM CTE

Result :

IS_EMPTY
--------
empty
empty
123

Solution 5

Try this:

ISNULL(IIF (ColunmValue!='',ColunmValue, 'no units exists') , 'no units exists') AS 'ColunmValueName' 
Share:
158,459
Bharath
Author by

Bharath

Updated on March 07, 2021

Comments

  • Bharath
    Bharath about 3 years
    if((isnull(@value,''))='')
    

    I want to know whether the above piece of code works in checking if the variable is null or empty.