IsEmpty functioning as ISNULL in SQL Server?

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 for the IsEmpty function as ISNULL. So that I can

 ISEMPTY(@SKU, NULL) 

Is this possible in SQL.

+9
source share
5 answers

Try NULLIF below

 NULLIF(@SKU,'') 
+39
source

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

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

+7
source

Try:

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

which returns NULL if the 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.

+7
source

This may be what you are 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 someone uses the code that I suggested, forget it and use NULLIF (), as the other guys suggested. I completely forgot that it exists.

+1
source
 SELECT ISNULL( CASE StringColum1 WHEN '' THEN NULL ELSE textcolum1 END ,textcolum2) 
0
source

Source: https://habr.com/ru/post/951113/


All Articles