25
if((isnull(@value,''))='')

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

4
  • That would depend on the type of @value. Commented Apr 18, 2013 at 10:14
  • 3
    @Pete: Not in SQL Server. Commented Apr 18, 2013 at 10:15
  • its a INT type. i want to consider 0 as valid.. want to eliminate only NULL and '' values Commented Apr 18, 2013 at 10:16
  • 1
    @Bharath '' is not a valid value for an INT, so no need to check for that case if the type is really INT. Commented Apr 18, 2013 at 10:18

9 Answers 9

45

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 ''.

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

3 Comments

Also important to know that you should stick to using "is null " or "is not null" when dealing with comparisons that may or not be null.
what about if (LEN(@value) is null) when @value is string?
@rivaldid: That's the same thing as checking if the value is null. The length of a string is only null when the string is null. If the string has a zero length, the len function returns zero, not null.
11

Use This way is Better

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

This check the field is empty or NULL

1 Comment

I like this but modified it: if ''=ISNULL(@Value,'')
9

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

Comments

1

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

Comments

1

Try this:

ISNULL(IIF (ColunmValue!='',ColunmValue, 'no units exists') , 'no units exists') AS 'ColunmValueName' 

Comments

1
    IF (LEN(@value) > 0) PRINT 'Variable is not null and not empty'

Comments

0

You can try

<column_name> is null

in the where clause.

1 Comment

Your answer could be made more to the person asking the question if you add more detail.
0

You can try this.....

DECLARE @value Varchar(100)=NULL
IF(@value = '' OR @value IS NULL)
  BEGIN
    select 1
  END
ELSE
  BEGIN
    select 0
  END

Comments

0
declare @sexo as char(1)

select @sexo='F'

select * from pessoa

where isnull(Sexo,0) =isnull(@Sexo,0)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.