String.IsNullOrEmpty like function for VARCHARs in SQL?

前端 未结 10 1929
一个人的身影
一个人的身影 2021-02-12 11:10

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:

10条回答
  •  我寻月下人不归
    2021-02-12 11:59

    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
    

提交回复
热议问题