Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
CSharp IsNullOrEmpty Function In SQL Server
From Wiki
Here is the SQL equivalent of the C# IsNotNullOrEmpty function
Here is my version which I have modified, you pass an additional parameter in to indicate whether you want blanks only to count or not
- CREATE FUNCTION dbo.IsNotNullOrEmpty(@TEXT NVARCHAR(4000),@BlanksIsEmpty BIT)
- RETURNS BIT
- AS
- BEGIN
- DECLARE @ReturnValue BIT
- IF @BlanksIsEmpty = 0
- BEGIN
- SELECT @ReturnValue= SIGN(COALESCE(DATALENGTH(@TEXT),0))
- END
- ELSE
- BEGIN
- SELECT @ReturnValue= SIGN(COALESCE(DATALENGTH(RTRIM(@TEXT)),0))
- END
- RETURN @ReturnValue
- END
- Go
Here are some calls where we want blanks to return as empty or null The function returns 0 if it is empty and 1 if it is not empty
- SELECT dbo.IsNotNullOrEmpty(null,1),dbo.IsNotNullOrEmpty('azas',1),
- dbo.IsNotNullOrEmpty(' ',1),dbo.IsNotNullOrEmpty('',1)
Here are some calls where we don't want blanks to return as empty or null
- SELECT dbo.IsNotNullOrEmpty(null,0),dbo.IsNotNullOrEmpty('azas',0),
- dbo.IsNotNullOrEmpty(' ',0),dbo.IsNotNullOrEmpty('',0)
Contributed by: --SQLDenis 16:38, 23 June 2008 (GMT)
Part of SQL Server Programming Hacks
Section Handy tricks



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.