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:
You don't need to check for null before calling LEN. You can just use LEN(@SomeVarcharParm) > 0. This will return false if the value is NULL, '', or ' '. This is because NULL > 0 returns false. See for yourself run:
SELECT
CASE WHEN NULL > 0 THEN 'NULL > 0 = true' ELSE 'NULL > 0 = false' END,
CASE WHEN LEN(NULL) > 0 THEN 'LEN(NULL) = true' ELSE 'LEN(NULL) = false' END,
CASE WHEN LEN('') > 0 THEN 'LEN('''') > 0 = true' ELSE 'LEN('''') > 0 = false' END,
CASE WHEN LEN(' ') > 0 THEN 'LEN('' '') > 0 = true' ELSE 'LEN('' '') > 0 = false' END,
CASE WHEN LEN(' test ') > 0 THEN 'LEN('' test '') > 0 = true' ELSE 'LEN('' test '') > 0 = false' END
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.
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.
I upvoted Atron's answer though he technically implemented IfNullOrWhiteSpace
.
Here's my implementation of IfNullOrEmpty()
:
IF EXISTS (SELECT * FROM sys .objects WHERE object_id = OBJECT_ID(N'[dbo].[IfNullOrEmpty]' ) and type in ( N'FN'))
DROP FUNCTION dbo.IfNullOrEmpty
go
CREATE FUNCTION dbo.IfNullOrEmpty(@value varchar(max), @substitute varchar(max)) returns varchar(max) as
BEGIN
IF @value IS NOT NULL AND LEN(@value) > 0
RETURN @value
RETURN @substitute
END
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
Here is my function that "extends" ISNULL and checks for empty as well. The test value is checked for null and if it is not null it is trimmed and then checked for length.
The function returns the test string if it is NOT Null or Empty, otherwise the second string is returned.
CREATE FUNCTION [dbo].[ISNULLOREMPTY]
(
@value NVARCHAR(max),
@return NVARCHAR(max)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
IF (@value IS NULL)
BEGIN
RETURN @return
END
ELSE
BEGIN
IF (LEN(LTRIM(@value)) = 0)
BEGIN
RETURN @return
END
END
RETURN @value;
END
GO