IsEmpty函数如SQL Server中的ISNULL?

时间:2020-12-28 15:45:17

I have this sql,

我有这个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函数,如ISNULL。所以我可以,

ISEMPTY(@SKU, NULL)

Is this is possible in SQL.

这在SQL中是否可行。

4 个解决方案

#1


37  

Try NULLIF as below

尝试使用NULLIF,如下所示

NULLIF(@SKU,'')

#2


7  

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

使用SET @SKU = NULLIF(@SKU,'')将@SKU设置为null,其中@SKU等于第二个参数的值。

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

IsEmpty不是内置的T-SQL函数,但NULLIF可用于实现类似的行为。

#3


6  

Please try:

请尝试:

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

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

如果value为NULL,空或空格,则返回NULL。

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.

注意:如果两个表达式不相等,则NULLIF返回第一个表达式。如果表达式是等效的,则NULLIF返回第一个表达式类型的空值。

#4


1  

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

EDIT2

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

如果有人使用我建议的那种代码,请忘记它,并像其他人建议的那样使用NULLIF()。我完全忘了它存在。

#1


37  

Try NULLIF as below

尝试使用NULLIF,如下所示

NULLIF(@SKU,'')

#2


7  

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

使用SET @SKU = NULLIF(@SKU,'')将@SKU设置为null,其中@SKU等于第二个参数的值。

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

IsEmpty不是内置的T-SQL函数,但NULLIF可用于实现类似的行为。

#3


6  

Please try:

请尝试:

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

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

如果value为NULL,空或空格,则返回NULL。

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.

注意:如果两个表达式不相等,则NULLIF返回第一个表达式。如果表达式是等效的,则NULLIF返回第一个表达式类型的空值。

#4


1  

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

EDIT2

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

如果有人使用我建议的那种代码,请忘记它,并像其他人建议的那样使用NULLIF()。我完全忘了它存在。