String.IsNullOrEmpty like function for VARCHARs in SQL?

前端 未结 10 1864
一个人的身影
一个人的身影 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:54

    If I'm concatenating or coalescing a string inline (within a select statement), and I want to check if the column is NULL or Empty, I do this:

    ISNULL('starting to build string ' 
    + NULLIF(some_table..some_col_that_might_be_null_or_empty, '')
    , 'string to append if the resulting concatenation is null')
    

    The NULLIF on the inner part of the expression will force the column to be NULL if it's empty, then the outer ISNULL expression can depend on consistent input and react accordingly.

提交回复
热议问题