• karthik82.vk (3/3/2015)


    Hi,

    I have created a function that will check whether the data is null or not. If its null then it will display that as No data else it will display the original value.

    Below is the function

    GO

    /****** Object: UserDefinedFunction [dbo].[fnchkNull] Script Date: 3/4/2015 12:01:58 PM ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE FUNCTION [dbo].[fnchkNull](@colname varchar(100))

    RETURNS varchar(50)

    AS

    BEGIN

    DECLARE @OutputString VARCHAR(255)

    IF LEN(@colname) = 0

    BEGIN

    SET @OutputString = ISNULL(@colname,'No Data')

    END

    IF Len(@colname) <> 0

    BEGIN

    IF @colname = 'NULL'

    BEGIN

    SET @OutputString = 'No Data'

    END

    SET @OutputString = @colname

    END

    RETURN ISNULL(@OutputString, 'No Data')

    END

    GO

    The code is working good. However i want the return type to be dynamic. If the data type supplied is integer then i want to return a integer value like 0 if its null. if the data value is varchar then i want to return 'No Data'

    I tried for this but not able to do it. Can anyone please help me?

    This is plain silly. It offers nothing over ISNULL and will slow your queries down.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden