Mladen Prajdić Blog

Blog about stuff and things and stuff. Mostly about SQL server and .Net

Sql Server: IsNullOrEmpty function

A coworker who works mainly in C# wanted to know if there's an IsNullOrEmpty function.

After a brief NO, i've given him this one.

So far it works great.

 

CREATE FUNCTION dbo.IsNullOrEmpty(@text NVARCHAR(4000))
RETURNS BIT
AS
BEGIN
    IF ISNULL(@text, '') = ''
    BEGIN
        RETURN 1
    END
<span class="kwrd">RETURN</span> 0

END

GO

DECLARE @text VARCHAR(100)

SELECT @text = 'gdrfash5' SELECT dbo.IsNullOrEmpty(@text)

SELECT @text = '' SELECT dbo.IsNullOrEmpty(@text)

SELECT @text = ' ' SELECT dbo.IsNullOrEmpty(@text)

SELECT @text = null SELECT dbo.IsNullOrEmpty(@text)

 

kick it on DotNetKicks.com

Legacy Comments


Bill Curnow
2007-06-13
re: Sql Server: IsNullOrEmpty function
This may be a cultural difference, but your third test should return false. Three ASCII 32 characters is not the same as nothing.

Mladen
2007-06-13
re: Sql Server: IsNullOrEmpty function
could be A cultural diff :)
i think it should return true because it holds no relevant data.

but if you don't want to you can easily add a replace before the comparison

Bill Curnow
2007-06-13
re: Sql Server: IsNullOrEmpty function
"Relevant" to the user, the programmer, or the computer? :)

This is really an artifact of garbage-in/garbage-out. If leading or trailing spaces are not relevant to the application, they should be trimmed before the data is inserted into the database. Of course, I'm probably preaching to the choir.

Mladen
2007-06-13
re: Sql Server: IsNullOrEmpty function
relevant to my current needs :)

i agree completly with you. you are preaching to the choir :))))

michael
2007-06-14
re: Sql Server: IsNullOrEmpty function
CREATE FUNCTION dbo.IsNullOrEmpty(@text VARCHAR(4000))
RETURNS BIT
AS

BEGIN
DECLARE @Return BIT

IF @text IS NULL
BEGIN
SET @Return = 1 --true
GOTO RET
END

IF DATALENGTH(@text)= DATALENGTH('') BEGIN
SET @Return = 1 --true
GOTO RET
END

RETURN 0 --false

RET:
RETURN @Return
END
GO

Tom
2007-06-14
re: Sql Server: IsNullOrEmpty function
IMO "DATALENGTH" is the way to go. :-)

Denis the SQL Menace
2007-06-14
re: Sql Server: IsNullOrEmpty function
Toooo much code which can be handled in 1 line by using the SIGN function

ALTER FUNCTION dbo.IsNullOrEmpty(@text NVARCHAR(4000))
RETURNS BIT
AS

BEGIN
RETURN SIGN(COALESCE(DATALENGTH(@text),0))
--Use this if you want blanks to return 0
--RETURN SIGN(COALESCE(DATALENGTH(RTRIM(@text)),0))
END
Go

SELECT dbo.IsNullOrEmpty(null),dbo.IsNullOrEmpty('azas'),dbo.IsNullOrEmpty(' '),dbo.IsNullOrEmpty('')

depending on what ' ' should return use the first or the second line

Denis

Mladen
2007-06-14
re: Sql Server: IsNullOrEmpty function
very nice!
Thanx Denis.

Alexander
2007-06-14
re: Sql Server: IsNullOrEmpty function
And then your coworker will happily run
select * from sometablewithmillionrows
wherre dbo.IsNullOrEmpty(somefield)=0

killing CPU on SQL Server....
Good luck

Mladen
2007-06-14
re: Sql Server: IsNullOrEmpty function
well... if we had a table with million rows then i'd be worried. :)

actually all we needed this function for was to help transform some data.