Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:45:14+00:00 2026-06-13T22:45:14+00:00

I was looking for a CLR function that will do the same thing as

  • 0

I was looking for a CLR function that will do the same thing as String.Format in C#. As an example, I would like to do the following through a CLR function:

String.Format("My name is {0}, and I live in {1}",myName, cityName)

I would like to be able to pass variable number of parameters after the first parameter, which would be as many place holders are specified in the first parameter to this CLR function. Anyone has any idea on what would be the C# code for such a CLR function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:45:16+00:00Added an answer on June 13, 2026 at 10:45 pm

    I’m looking for the same thing. I came across these two sites: http://www.fotia.co.uk/fotia/Blog/2009/05/indispensable-sql-clr-functions.html & http://stringformat-in-sql.blogspot.com/.

    The first uses a CLR function, the other uses a stored procedure.

    In the first post, the writer says that UDF parameters aren’t optional so you can’t have variable number of params (at least the way he is doing it). He just creates a different function with the number or args he will need.

    [SqlFunction(DataAccess = DataAccessKind.None)]
    public static SqlString FormatString1(SqlString format, object arg0, object arg1)
    {
       return format.IsNull ? SqlString.Null : 
           string.Format(format.Value, SqlTypeToNetType(arg0, arg1));
    }

    EDIT : This is how I solved this problem for me.

    Using the stored procedure from the second article, I created this function to format the text for me using a pivot table. Our database is setup with a table dbo.[Rules] that has a column [Description] that has a string that needs to be formatted such as: “NET INCOME MARGINAL (${0} TO BELOW ${1})”. We then have a 1-many table dbo.[RuleParameters] with a row for each value that needs to be substituted. The column dbo.[SortOrder] specifies which order the arguments go in. This function will work for AT MOST 4 arguments which is the most that we have.

    CREATE FUNCTION [dbo].[GetRuleDescriptionWithParameters]
    (
        @Code VARCHAR(3)
    )
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
    DECLARE @Result NVARCHAR(400)
    
        SELECT 
            @Result =
                dbo.FormatString([Description], 
                    ISNULL([1], '') + ',' + 
                    ISNULL([2], '') + ',' + 
                    ISNULL([3], '') + ',' + 
                    ISNULL([4], '')
                    )
        FROM
        (
            SELECT 
            r.[Description]
            ,rp.Value
            ,rp.SortOrder
    
            FROM 
                [Rules] r
            LEFT OUTER JOIN [RuleParameters] rp
                ON r.Id = rp.RuleId
    
            WHERE r.Code = @Code
    
        ) AS SourceTable
        PIVOT
        (
            MAX(Value)
            FOR SortOrder IN ([1], [2], [3], [4])
        ) AS PivotTable;
    
    
        RETURN @Result
    END
    

    EDIT #2: Adding more examples

    Format string function:

    CREATE FUNCTION [dbo].[FormatString]
    (
        @Format NVARCHAR(4000) ,
        @Parameters NVARCHAR(4000)
    )
    RETURNS NVARCHAR(MAX)
    AS
    BEGIN
    DECLARE @Message NVARCHAR(400),
    @Delimiter CHAR(1)
    DECLARE @ParamTable TABLE ( ID INT IDENTITY(0,1), Paramter VARCHAR(1000) )
    SELECT @Message = @Format, @Delimiter = ','
    ;WITH CTE (StartPos, EndPos) AS
    (
        SELECT 1, CHARINDEX(@Delimiter, @Parameters)
        UNION ALL
        SELECT EndPos + (LEN(@Delimiter)), CHARINDEX(@Delimiter,@Parameters, EndPos + (LEN(@Delimiter)))
    FROM CTE
    WHERE EndPos > 0
    )
    INSERT INTO @ParamTable ( Paramter )
    SELECT
    [ID] = SUBSTRING ( @Parameters, StartPos, CASE WHENEndPos > 0 THEN EndPos - StartPos ELSE 4000 END )
    FROM CTE
    UPDATE @ParamTable SET @Message = REPLACE ( @Message, '{'+CONVERT(VARCHAR,ID) + '}',     Paramter )
    RETURN @Message
    END
    GO
    

    Here is how I call the stored procedure:

    SELECT r.[Id]
            ,[Code]
            ,[Description]
            ,dbo.GetRuleDescriptionWithParameters([Code])
            ,rp.[Value]
            ,rp.SortOrder
    FROM [Rules] r
    INNER JOIN [RuleParameters] rp
        ON r.Id = rp.RuleId
    

    Now I can just call the function like this to get everything formatted nicely for me! Here is sample usage with the results:

    Id  Code    Description                     (No column name)          Value     SortOrder
    1   1       NOT THE MINIMUM AGE             NOT THE MINIMUM AGE       18        1
    3   8       NET INCOME (BELOW ${0})      NET INCOME (BELOW $400)   400    1
    4   9       NET (${0} TO BELOW ${1})          NET ($400 TO BELOW $600)  400         1
    4   9       NET (${0} TO BELOW ${1})          NET ($400 TO BELOW $600)  600         2
    

    Normally when calling this, I wouldn’t join on the [RuleParameters] table, but I did in this case so that you can see the data before and after in one data set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a function that would be able to do the same
Looking for a Smalltalk compiler that given Smalltalk (Instantiations) will emit either Java bytecode,
I am developing an app that uses DatePicker and LoopSelector at same time. Looking
I have some utilities and extension methods that I would like to leverage in
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking for a perl one-liner what will find all words with the next pattern:
Looking at the Ehcahce implementation of net.sf.cache.JS107, I am trying to achieve the following
Looking for best advice on how to do this: I have an insert like
Looking for a control that allows to select one text value at a time
I'd like to call methods of a class dynamically with parameter values that are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.