String.IsNullOrEmpty like function for VARCHARs in SQL?

Viewed 33307

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:

IF @SomeVarcharParm IS NOT NULL AND LEN(@SomeVarcharParm) > 0
BEGIN
    -- do stuff
END

There's gotta be a better way to do this. Isn't there?

10 Answers

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
Related