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

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

SteveC
  • 15,808
  • 23
  • 102
  • 173
Bharath
  • 573
  • 1
  • 7
  • 20

9 Answers9

44

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

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 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. – Eduardo La Hoz Miranda Jun 08 '15 at 21:07
  • what about if (LEN(@value) is null) when @value is string? – rivaldid May 28 '21 at 18:59
  • @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. – Guffa Jul 05 '21 at 10:45
11

Use This way is Better

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

This check the field is empty or NULL

Gwenc37
  • 2,064
  • 7
  • 18
  • 22
AnasChavadi
  • 427
  • 1
  • 4
  • 10
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

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
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
bvr
  • 4,786
  • 1
  • 20
  • 24
1

Try this:

ISNULL(IIF (ColunmValue!='',ColunmValue, 'no units exists') , 'no units exists') AS 'ColunmValueName' 
benomatis
  • 5,536
  • 7
  • 36
  • 59
milan
  • 11
  • 1
1
    IF (LEN(@value) > 0) PRINT 'Variable is not null and not empty'
0

You can try

<column_name> is null

in the where clause.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
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
Athafoud
  • 2,898
  • 3
  • 40
  • 58
0
declare @sexo as char(1)

select @sexo='F'

select * from pessoa

where isnull(Sexo,0) =isnull(@Sexo,0)
Sabri Karagönen
  • 2,212
  • 1
  • 14
  • 28