20

I have this sql,

IF(@ID = '')
BEGIN
    SET @ID = NULL;
END

IF(@Name = '')
BEGIN
    SET @Name = NULL;
END

IF(@PhoneNumber = '')
BEGIN
    SET @PhoneNumber = NULL;     
END

IF(@Price = '')
BEGIN
    SET @Price = NULL;
END

IF(@NewPrice = '')
BEGIN
    SET @NewPrice = NULL;
END

IF(@ModelNumber = '')
BEGIN
    SET @ModelNumber = NULL;
END

IF(@SKU = '')
BEGIN
    SET @SKU = NULL;
END

I am looking IsEmpty function like ISNULL. So that I can,

ISEMPTY(@SKU, NULL)

Is this is possible in SQL.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322

9 Answers9

63

Try NULLIF as below

NULLIF(@SKU,'')
Robert
  • 25,425
  • 8
  • 67
  • 81
11

Please try:

SET @YourValue=ISNULL(NULLIF(@YourValue,' '), NULL)

which returns NULL if value is NULL, empty or space.

Note: NULLIF returns the first expression if the two expressions are not equivalent. If the expressions are equivalent, NULLIF returns a null value of the type of the first expression.

TechDo
  • 18,398
  • 3
  • 51
  • 64
  • 2
    isn't IsNull(,NULL) redundant ? – t-clausen.dk Aug 06 '13 at 11:31
  • Yeah... We can avoid that. – TechDo Aug 06 '13 at 11:47
  • might seem redundant, but it is the answer to the question in the title. if we replace the NULL in the parentheses to , we get a perfectly functional IsEmpty function. and that is exactly what I was looking for. the following code changes empty strings to an error message: `ISNULL(NULLIF(@YourValue,''), 'No value entered')` – robotik Dec 15 '15 at 17:23
7

Use SET @SKU = NULLIF(@SKU,'') to set @SKU to null where @SKU equals the value of the second argument.

IsEmpty isn't a built-in T-SQL function, but NULLIF can be used to achieve a similar behavior.

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
Mentatmatt
  • 515
  • 5
  • 13
2

This may be what you're looking for:

SET @SKU = CASE @SKU WHEN '' THEN NULL ELSE @SKU END

EDIT

For all your variables...

SELECT
  @ID = CASE @ID WHEN '' THEN NULL ELSE @ID END,
  @Name = CASE @Name WHEN '' THEN NULL ELSE @Name END,
  @PhoneNumber = CASE @PhoneNumber WHEN '' THEN NULL ELSE @PhoneNumber END,
  @Price = CASE @Price WHEN '' THEN NULL ELSE @Price END,
  @NewPrice = CASE @NewPrice WHEN '' THEN NULL ELSE @NewPrice END,
  @ModelNumber = CASE @ModelNumber WHEN '' THEN NULL ELSE @ModelNumber END,
  @SKU = CASE @SKU WHEN '' THEN NULL ELSE @SKU END¸

EDIT2

If anyone uses the kind of code I suggested, forget it and use NULLIF() as other guys suggested. I COMPLETELY FORGOT it exists.

OzrenTkalcecKrznaric
  • 5,535
  • 4
  • 34
  • 57
1

Try below in select clause.

select ISNULL( nullif(EmptyOrNullOrBlankColumn,'' ),NULL)

Below in Where Clause.

where ISNULL( nullif(EmptyOrNullOrBlankColumn,'' ),NULL) is not null
adiga
  • 34,372
  • 9
  • 61
  • 83
ramit girdhar
  • 2,272
  • 1
  • 25
  • 26
1

Personally I don't like the nested NULLIF and ISNULL in some of the other answers. I find this logic difficult to follow and prefer using a simple IIF( condition, true_value, else_value )

SQL uses three valued logic true, false and unknown.

With IIF if the condition evaluates to true you go through to the first case, otherwise (when it is false or unknown) you go through to the else case.

Comparisons with NULL (whether = or <> or LIKE or NOT LIKE) will evaluate as unknown.

So you just have to take care to think about the semantics you want and express it in a manner that empty string will be either bucketed with or without NULL as desired.

One other thing to be aware of is that SQL Server usually ignores trailing spaces for LEN or string comparisons. It does treat them as significant in the value on the LHS of LIKE however.

So the following expressions provide a variety of different semantics to choose the desired one from.

SELECT 
  ValueUnderTest
 ,StringLength = DATALENGTH(ValueUnderTest)
 ,IsEmpty = IIF(ValueUnderTest = '', 'True', 'False') 
 ,IsNullOrEmpty = IIF(ValueUnderTest <> '', 'False', 'True')
 ,IsZeroLength = IIF('' LIKE ValueUnderTest, 'True', 'False')
 ,IsNullOrZeroLength = IIF('' NOT LIKE ValueUnderTest, 'False', 'True')
 FROM
 (
 VALUES (''),  
        (' '), 
        (NULL), 
        ('Fish')
 ) TestData(ValueUnderTest)

Returns

ValueUnderTest StringLength IsEmpty IsNullOrEmpty IsZeroLength IsNullOrZeroLength
0 True True True True
1 True True False False
NULL NULL False True False True
Fish 4 False False False False
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
0

if you consider ' ' as empty string you can use

LEN(RTRIM(ISNULL(FieldName,''))) > 0

0

When what you want is a boolean result e.g. Y/N or true/false such as in a report, then such suggested simple statements such as NULLIF will not help. Here is a solution:

case when LEN(isnull(iv.value2, 'Y')) = 0 then 'Y' else 'N' end isEmpty
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
  • If `iv.value2` is null then `isnull(iv.value2, 'Y')` is `Y` and `LEN` of that will be `1` - so according to this expression null will return `N` for `IsEmpty` – Martin Smith Feb 02 '23 at 22:03
  • To return `Y` for strings that are equal to `''` or are null you can just use `IIF(iv.value2 <> '', 'N', 'Y') AS isEmpty` – Martin Smith Feb 02 '23 at 22:06
  • @MartinSmith, **Intent**: Return Y only when the string is '', N for null or anything else **Question Posted**: "IsEmpty function like ISNULL in SQL Server?". **Understanding**: Imran wants to know if a string is empty, and not whether it is empty or null. **Imran's comment**: I am checking Empty not null – Imran Qadir Baksh - Baloch Aug 6, 2013 at 11:08 **IsEmpty**: This camel-case "function" indicates a programmer, and boolean results are expected. **Application**: I developed, tested and use the solution in a sql-generated report to differentiate empty vs. null values. – user6186047 Feb 04 '23 at 00:00
  • You might be right about what the OP wanted. For that interpretation you can just use `IIF(iv.value2 = '', 'Y', 'N') AS isEmpty` - if `value2` is `NULL` then you will go to the `ELSE` branch anyway. – Martin Smith Feb 04 '23 at 00:10
  • 1
    @MartinSmith, your recommended enhancement with IIF is definitely the ideal solution - thanks. Unfortunately I don't have enough of a reputation (LOL) to upvote your comment – user6186047 Feb 05 '23 at 23:18
-1
SELECT ISNULL( CASE StringColum1  WHEN '' THEN NULL ELSE textcolum1 END ,textcolum2) 
Nick
  • 138,499
  • 22
  • 57
  • 95