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:
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