23

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.

9 Answers 9

68

Try NULLIF as below

NULLIF(@SKU,'')
Sign up to request clarification or add additional context in comments.

4 Comments

I am checking Empty not null
English version of above NULLIF page here: technet.microsoft.com/en-gb/library/…
@GazB I've changed, Thanks!
ISNULL(NULLIF(@SKU,''),'SKU if empty')
13

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.

3 Comments

isn't IsNull(<any value in the world>,NULL) redundant ?
Yeah... We can avoid that.
might seem redundant, but it is the answer to the question in the title. if we replace the NULL in the parentheses to <any other value>, 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')
8

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.

Comments

3

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.

Comments

2

Try below in select clause.

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

Below in Where Clause.

where ISNULL( nullif(EmptyOrNullOrBlankColumn,'' ),NULL) is not null

Comments

2

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

Comments

0

if you consider ' ' as empty string you can use

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

Comments

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

5 Comments

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
To return Y for strings that are equal to '' or are null you can just use IIF(iv.value2 <> '', 'N', 'Y') AS isEmpty
@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.
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.
@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
-1
SELECT ISNULL( CASE StringColum1  WHEN '' THEN NULL ELSE textcolum1 END ,textcolum2) 

1 Comment

dont write only code. explanation is also necessary

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.