String.IsNullOrEmpty like function for VARCHARs in SQL?

前端 未结 10 1907
一个人的身影
一个人的身影 2021-02-12 11:10

Say I\'ve got a function or stored procedure that takes in several VARCHAR parameters. I\'ve gotten tired of writing SQL like this to test if these parameters have a value:

10条回答
  •  梦如初夏
    2021-02-12 11:58

    I realize this is an old question, but this is what I use in MSSQL: LEN(ISNULL(@asdf, ''))>0

    Example:

    DECLARE @asdf varchar(10)
    SET @asdf = NULL --You can change this value to test different outputs
    
    BEGIN IF LEN(ISNULL(@asdf, '')) > 0
       PRINT @asdf
    ELSE
       PRINT 'IS NullOrEmpty'
    END
    
    --You can use it inline like this:
    PRINT CASE LEN(ISNULL(@asdf, '')) WHEN 0 THEN 'IS NullOrEmpty' ELSE @asdf END
    

    I think this is simpler and more straight forward than the other solutions because it is literally checking if the string is null or has a length of 0.

提交回复
热议问题