Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Navigation

Google Ads

CSharp IsNullOrEmpty Function In SQL Server

From Wiki

Jump to: navigation, search

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

  1. CREATE FUNCTION dbo.IsNotNullOrEmpty(@TEXT NVARCHAR(4000),@BlanksIsEmpty BIT)
  2. RETURNS BIT
  3. AS
  4.  
  5. BEGIN
  6. DECLARE @ReturnValue BIT
  7.  
  8. IF @BlanksIsEmpty = 0
  9. BEGIN
  10. SELECT @ReturnValue= SIGN(COALESCE(DATALENGTH(@TEXT),0))
  11. END
  12. ELSE
  13. BEGIN
  14. SELECT @ReturnValue= SIGN(COALESCE(DATALENGTH(RTRIM(@TEXT)),0))
  15. END
  16.  
  17. RETURN @ReturnValue
  18. END
  19. 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

  1. SELECT dbo.IsNotNullOrEmpty(null,1),dbo.IsNotNullOrEmpty('azas',1),
  2. dbo.IsNotNullOrEmpty(' ',1),dbo.IsNotNullOrEmpty('',1)


Here are some calls where we don't want blanks to return as empty or null

  1. SELECT dbo.IsNotNullOrEmpty(null,0),dbo.IsNotNullOrEmpty('azas',0),
  2. dbo.IsNotNullOrEmpty(' ',0),dbo.IsNotNullOrEmpty('',0)


Contributed by: --SQLDenis 16:38, 23 June 2008 (GMT)

Part of SQL Server Programming Hacks

Section Handy tricks

417 Rating: 1.6/5 (8 votes cast)