String.Format like functionality in T-SQL?

后端 未结 13 1999
滥情空心
滥情空心 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:13

    There is a way, but it has its limitations. You can use the FORMATMESSAGE() function. It allows you to format a string using formatting similar to the printf() function in C.

    However, the biggest limitation is that it will only work with messages in the sys.messages table. Here's an article about it: microsoft_library_ms186788

    It's kind of a shame there isn't an easier way to do this, because there are times when you want to format a string/varchar in the database. Hopefully you are only looking to format a string in a standard way and can use the sys.messages table.

    Coincidentally, you could also use the RAISERROR() function with a very low severity, the documentation for raiseerror even mentions doing this, but the results are only printed. So you wouldn't be able to do anything with the resulting value (from what I understand).

    Good luck!

提交回复
热议问题