String.Format like functionality in T-SQL?

后端 未结 13 1992
滥情空心
滥情空心 2020-12-02 12:54

I\'m looking for a built-in function/extended function in T-SQL for string manipulation similar to the String.Format method in .NET.

13条回答
  •  悲哀的现实
    2020-12-02 13:27

    here's what I found with my experiments using the built-in

    FORMATMESSAGE() function

    sp_addmessage @msgnum=50001,@severity=1,@msgText='Hello %s you are #%d',@replace='replace'
    SELECT FORMATMESSAGE(50001, 'Table1', 5)
    

    when you call up sp_addmessage, your message template gets stored into the system table master.dbo.sysmessages (verified on SQLServer 2000).

    You must manage addition and removal of template strings from the table yourself, which is awkward if all you really want is output a quick message to the results screen.

    The solution provided by Kathik DV, looks interesting but doesn't work with SQL Server 2000, so i altered it a bit, and this version should work with all versions of SQL Server:

    IF OBJECT_ID( N'[dbo].[FormatString]', 'FN' ) IS NOT NULL
        DROP FUNCTION [dbo].[FormatString]
    GO
    /***************************************************
    Object Name : FormatString
    Purpose : Returns the formatted string.
    Original Author : Karthik D V http://stringformat-in-sql.blogspot.com/
    Sample Call:
    SELECT dbo.FormatString ( N'Format {0} {1} {2} {0}', N'1,2,3' )
    *******************************************/
    CREATE FUNCTION [dbo].[FormatString](
    @Format NVARCHAR(4000) ,
    @Parameters NVARCHAR(4000)
    )
    RETURNS NVARCHAR(4000)
    AS
    BEGIN
        --DECLARE @Format NVARCHAR(4000), @Parameters NVARCHAR(4000) select @format='{0}{1}', @Parameters='hello,world'
        DECLARE @Message NVARCHAR(400), @Delimiter CHAR(1)
        DECLARE @ParamTable TABLE ( ID INT IDENTITY(0,1), Parameter VARCHAR(1000) )
        Declare @startPos int, @endPos int
        SELECT @Message = @Format, @Delimiter = ','
    
        --handle first parameter
         set @endPos=CHARINDEX(@Delimiter,@Parameters)
        if (@endPos=0 and @Parameters is not null) --there is only one parameter
            insert into @ParamTable (Parameter) values(@Parameters)
        else begin
            insert into @ParamTable (Parameter) select substring(@Parameters,0,@endPos)
        end
    
        while @endPos>0
        Begin
            --insert a row for each parameter in the 
            set @startPos = @endPos + LEN(@Delimiter)
            set @endPos = CHARINDEX(@Delimiter,@Parameters, @startPos)
            if (@endPos>0)
                insert into @ParamTable (Parameter) select substring(@Parameters,@startPos,@endPos)
            else
                insert into @ParamTable (Parameter) select substring(@Parameters,@startPos,4000)            
        End
    
        UPDATE @ParamTable SET @Message = REPLACE ( @Message, '{'+CONVERT(VARCHAR,ID) + '}', Parameter )
        RETURN @Message
    END
    Go
        grant execute,references on dbo.formatString to public
    

    Usage:

    print dbo.formatString('hello {0}... you are {1}','world,good')
    --result: hello world... you are good
    

提交回复
热议问题