How do check if a parameter is empty or null in Sql Server stored procedure in IF statement?

后端 未结 3 628
粉色の甜心
粉色の甜心 2021-02-07 02:13

I read this: How do I check if a Sql server string is null or empty but it not helped me in this situation.

The piece of code from my stored procedure:

I         


        
相关标签:
3条回答
  • 2021-02-07 02:59

    that is the right behavior.

    if you set @item1 to a value the below expression will be true

    IF (@item1 IS NOT NULL) OR (LEN(@item1) > 0)
    

    Anyway in SQL Server there is not a such function but you can create your own:

    CREATE FUNCTION dbo.IsNullOrEmpty(@x varchar(max)) returns bit as
    BEGIN
    IF @SomeVarcharParm IS NOT NULL AND LEN(@SomeVarcharParm) > 0
        RETURN 0
    ELSE
        RETURN 1
    END
    
    0 讨论(0)
  • 2021-02-07 02:59

    To check if variable is null or empty use this:

    IF LEN(ISNULL(@var, '')) = 0
        -- Is empty or NULL
    ELSE
        -- Is not empty and is not NULL
    
    0 讨论(0)
  • 2021-02-07 03:01

    Of course that works; when @item1 = N'', it IS NOT NULL.

    You can define @item1 as NULL by default at the top of your stored procedure, and then not pass in a parameter.

    0 讨论(0)
提交回复
热议问题