String.IsNullOrEmpty like function for VARCHARs in SQL?

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

    please use case command.case structure is:

    case when condition then 'some value' else 'some other value' end

    some example:

    declare @SomeVarcharParm as nvarchar(20)=''
    declare @SomeVarcharParm2 as nvarchar(20)=''
    
    --select 1 or 2
    select case when @SomeVarcharParm is null or @SomeVarcharParm='' then 1 else 2 end
    
    
    --if null or empty set 'empty' value else set @SomeVarcharParm value
    select @SomeVarcharParm2=case when @SomeVarcharParm is null or @SomeVarcharParm='' then 'empty' else @SomeVarcharParm end
    
    
    --use null or empty in update command
    update tbl1
    set field1=case when field1 is null or field1='' then @SomeVarcharParm2 else field1 end
    where id=1
    

提交回复
热议问题