Skip to content

Instantly share code, notes, and snippets.

@zonaro
Last active March 30, 2023 11:59
Show Gist options
  • Save zonaro/a9674486dcc89c4d2fea4ec6d6091eab to your computer and use it in GitHub Desktop.
Save zonaro/a9674486dcc89c4d2fea4ec6d6091eab to your computer and use it in GitHub Desktop.
IfBlank function for SQL Server
-- =============================================
-- Author: Zonaro
-- Create date: 30/03/2023
-- Description: Return a replacement value if expression is null or contains only whitespace
-- =============================================
CREATE FUNCTION IfBlank
(
@expression varchar(max),
@replacement varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
RETURN isnull(nullif(ltrim(rtrim(@expression)),''),@replacement)
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment