//
You're reading...
SQL

IsNullOrEmpty – A user Defined Function in SQL server

This is an equal implementation of C# function String.IsNullOrEmpty in SQL. This function returns 1 if parameter is NULL or empty.

Here is the implementation of this function:

Implementation:

CREATE FUNCTION dbo.IsNULLOrEmpty(@data varchar(max)) RETURNS BIT
AS
BEGIN 
       if(NULLIF(@data,'')) is NULL 
             return 1   -- return 1 when there is a NULL or Empty.
       return 0  -- return zero otherwise
END
GO

How to Use It:

Here is a quick implementation:

  declare @data1 varchar(max);  set @data1 = 'some test value here'
  declare @data2 varchar(max);  set @data2 = ''
  declare @data3 varchar(max);  set @data3 = NULL
  print dbo.IsNULLOrEmpty (@data1)
  print dbo.IsNULLOrEmpty (@data2)
  print dbo.IsNULLOrEmpty (@data3)

Results:

0
1
1
Advertisements

About Shahan Ayyub

Shahan is a Microsoft Certified Professional of .Net. Mostly self taught programmer and a quick learner, who just love to code and always eager to find unique solutions to the common problems. Currently working as an iOS developer.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: