Check if a parameter is null or empty in a stored procedure

前端 未结 12 1038
轮回少年
轮回少年 2020-12-02 22:09

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 n

相关标签:
12条回答
  • 2020-12-02 22:38

    Here is the general pattern:

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

    '' is an empty string in SQL Server.

    0 讨论(0)
  • 2020-12-02 22:40

    I recommend checking for invalid dates too:

    set @PreviousStartDate=case ISDATE(@PreviousStartDate) 
        when 1 then @PreviousStartDate 
            else '1/1/2010'
        end
    
    0 讨论(0)
  • 2020-12-02 22:44

    You can try this:-

    IF NULLIF(ISNULL(@PreviousStartDate,''),'') IS NULL
    SET @PreviousStartdate = '01/01/2010'
    
    0 讨论(0)
  • 2020-12-02 22:45

    To check if variable is null or empty use this

    IF(@PreviousStartDate IS NULL OR @PreviousStartDate = '')
    
    0 讨论(0)
  • 2020-12-02 22:47

    Another option:

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

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

    0 讨论(0)
  • 2020-12-02 22:51

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

    IF COALESCE(PATINDEX('%[^ ]%', @parameter), 0) > 0
        RAISERROR ...
    
    0 讨论(0)
提交回复
热议问题