检查存储过程中参数是空还是空

时间:2022-05-05 03:33:32

I know how to check if a parameter is null but i am not sure how to check if its empty ... I have these parameters and I want to check the previous parameters are empty or null and then set them like below

我知道如何检查一个参数是否为null但我不知道如何检查它是否为空...我有这些参数,我想检查以前的参数是空的还是null然后设置如下

ALTER PROCEDURE [dbo].[GetSummary]
    @PreviousStartDate NVARCHAR(50) ,
    @PreviousEndDate NVARCHAR(50) ,
    @CurrentStartDate NVARCHAR(50) ,
    @CurrentEndDate NVARCHAR(50)
AS
  BEGIN
    IF(@PreviousStartDate IS NULL OR EMPTY)
        SET @PreviousStartdate = '01/01/2010'  for example..

I would appreciate the help.

我很感激帮助。

11 个解决方案

#1


61  

I sometimes use NULLIF like so...

我有时会像这样使用NULLIF ......

IF NULLIF(@PreviousStartDate, '') IS NULL

There's probably no reason it's better than the way suggested by @Oded and @bluefeet, just stylistic preference.

可能没有理由比@Oded和@bluefeet建议的方式更好,只是风格偏好。

@danihp's method is really cool but my tired old brain wouldn't go to COALESCE when I'm thinking is null or empty :-)

@ danihp的方法非常酷,但是当我想的是空的或空的时候,我疲惫的老脑子不会去COALESCE :-)

#2


27  

Here is the general pattern:

这是一般模式:

IF(@PreviousStartDate IS NULL OR @PreviousStartDate = '')

'' is an empty string in SQL Server.

''是SQL Server中的空字符串。

#3


11  

I use coalesce:

我使用coalesce:

IF ( COALESCE( @PreviousStartDate, '' ) = '' ) ...

#4


3  

What about combining coalesce and nullif?

结合coalesce和nullif怎么样?

SET @PreviousStartDate = coalesce(nullif(@PreviousStartDate, ''), '01/01/2010')

#5


2  

you can use:

您可以使用:

IF(@PreviousStartDate IS NULL OR @PreviousStartDate = '')

#6


2  

Another option:

另外一个选择:

IF ISNULL(@PreviousStartDate, '') = '' ...

see a function based on this expression at http://weblogs.sqlteam.com/mladenp/archive/2007/06/13/60231.aspx

在http://weblogs.sqlteam.com/mladenp/archive/2007/06/13/60231.aspx上查看基于此表达式的函数

#7


1  

If you want to use a parameter is Optional so use it.

如果要使用参数是可选的,请使用它。

CREATE PROCEDURE uspGetAddress @City nvarchar(30) = NULL, @AddressLine1 nvarchar(60) = NULL
    AS
    SELECT *
    FROM AdventureWorks.Person.Address
    WHERE City = ISNULL(@City,City)
    AND AddressLine1 LIKE '%' + ISNULL(@AddressLine1 ,AddressLine1) + '%'
    GO

#8


1  

To check if variable is null or empty use this:

要检查变量是null还是空,请使用以下命令:

IF LEN(ISNULL(@var, '')) = 0

#9


0  

I recommend checking for invalid dates too:

我也建议检查无效日期:

set @PreviousStartDate=case ISDATE(@PreviousStartDate) 
    when 1 then @PreviousStartDate 
        else '1/1/2010'
    end

#10


0  

If you want a "Null, empty or white space" check, you can avoid unnecessary string manipulation with LTRIM and RTRIM like this.

如果你想要一个“Null,empty或white space”检查,你可以避免像这样使用LTRIM和RTRIM进行不必要的字符串操作。

IF COALESCE(PATINDEX('%[^ ]%', @parameter), 0) > 0
    RAISERROR ...

#11


0  

You can try this:-

你可以试试这个: -

IF NULLIF(ISNULL(@PreviousStartDate,''),'') IS NULL
SET @PreviousStartdate = '01/01/2010'

#1


61  

I sometimes use NULLIF like so...

我有时会像这样使用NULLIF ......

IF NULLIF(@PreviousStartDate, '') IS NULL

There's probably no reason it's better than the way suggested by @Oded and @bluefeet, just stylistic preference.

可能没有理由比@Oded和@bluefeet建议的方式更好,只是风格偏好。

@danihp's method is really cool but my tired old brain wouldn't go to COALESCE when I'm thinking is null or empty :-)

@ danihp的方法非常酷,但是当我想的是空的或空的时候,我疲惫的老脑子不会去COALESCE :-)

#2


27  

Here is the general pattern:

这是一般模式:

IF(@PreviousStartDate IS NULL OR @PreviousStartDate = '')

'' is an empty string in SQL Server.

''是SQL Server中的空字符串。

#3


11  

I use coalesce:

我使用coalesce:

IF ( COALESCE( @PreviousStartDate, '' ) = '' ) ...

#4


3  

What about combining coalesce and nullif?

结合coalesce和nullif怎么样?

SET @PreviousStartDate = coalesce(nullif(@PreviousStartDate, ''), '01/01/2010')

#5


2  

you can use:

您可以使用:

IF(@PreviousStartDate IS NULL OR @PreviousStartDate = '')

#6


2  

Another option:

另外一个选择:

IF ISNULL(@PreviousStartDate, '') = '' ...

see a function based on this expression at http://weblogs.sqlteam.com/mladenp/archive/2007/06/13/60231.aspx

在http://weblogs.sqlteam.com/mladenp/archive/2007/06/13/60231.aspx上查看基于此表达式的函数

#7


1  

If you want to use a parameter is Optional so use it.

如果要使用参数是可选的,请使用它。

CREATE PROCEDURE uspGetAddress @City nvarchar(30) = NULL, @AddressLine1 nvarchar(60) = NULL
    AS
    SELECT *
    FROM AdventureWorks.Person.Address
    WHERE City = ISNULL(@City,City)
    AND AddressLine1 LIKE '%' + ISNULL(@AddressLine1 ,AddressLine1) + '%'
    GO

#8


1  

To check if variable is null or empty use this:

要检查变量是null还是空,请使用以下命令:

IF LEN(ISNULL(@var, '')) = 0

#9


0  

I recommend checking for invalid dates too:

我也建议检查无效日期:

set @PreviousStartDate=case ISDATE(@PreviousStartDate) 
    when 1 then @PreviousStartDate 
        else '1/1/2010'
    end

#10


0  

If you want a "Null, empty or white space" check, you can avoid unnecessary string manipulation with LTRIM and RTRIM like this.

如果你想要一个“Null,empty或white space”检查,你可以避免像这样使用LTRIM和RTRIM进行不必要的字符串操作。

IF COALESCE(PATINDEX('%[^ ]%', @parameter), 0) > 0
    RAISERROR ...

#11


0  

You can try this:-

你可以试试这个: -

IF NULLIF(ISNULL(@PreviousStartDate,''),'') IS NULL
SET @PreviousStartdate = '01/01/2010'