空或空检查字符串变量

时间:2021-06-17 20:29:27
if((isnull(@value,''))='')

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

我想知道上面的代码是否可以检查变量是null还是空。

7 个解决方案

#1


31  

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:

有了@value是int值的附加信息,您需要:

if (@value is null)

An int value can never contain the value ''.

int值永远不能包含值''。

#2


8  

Use This way is Better

使用这种方式更好

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

This check the field is empty or NULL

此检查字段为空或NULL

#3


7  

Yes, you could also use COALESCE(@value,'')='' which is based on the ANSI SQL standard:

是的,你也可以使用基于ANSI SQL标准的COALESCE(@ value,'')='':

SELECT CASE WHEN COALESCE(@value,'')='' 
    THEN 'Yes, it is null or empty' ELSE 'No, not null or empty' 
    END AS IsNullOrEmpty

DEMO

DEMO

#4


1  

Yes, it works. Check the below example. Assuming @value is not int

是的,它有效。请查看以下示例。假设@value不是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

#5


1  

Try this:

尝试这个:

ISNULL(IIF (ColunmValue!='',ColunmValue, 'no units exists') , 'no units exists') AS 'ColunmValueName' 

#6


0  

You can try

你可以试试

<column_name> is null

in the where clause.

在where子句中。

#7


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

#1


31  

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:

有了@value是int值的附加信息,您需要:

if (@value is null)

An int value can never contain the value ''.

int值永远不能包含值''。

#2


8  

Use This way is Better

使用这种方式更好

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

This check the field is empty or NULL

此检查字段为空或NULL

#3


7  

Yes, you could also use COALESCE(@value,'')='' which is based on the ANSI SQL standard:

是的,你也可以使用基于ANSI SQL标准的COALESCE(@ value,'')='':

SELECT CASE WHEN COALESCE(@value,'')='' 
    THEN 'Yes, it is null or empty' ELSE 'No, not null or empty' 
    END AS IsNullOrEmpty

DEMO

DEMO

#4


1  

Yes, it works. Check the below example. Assuming @value is not int

是的,它有效。请查看以下示例。假设@value不是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

#5


1  

Try this:

尝试这个:

ISNULL(IIF (ColunmValue!='',ColunmValue, 'no units exists') , 'no units exists') AS 'ColunmValueName' 

#6


0  

You can try

你可以试试

<column_name> is null

in the where clause.

在where子句中。

#7


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