Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Similar function in SQL Server to string.format in c#

In C#, if I do:

string code = string.format("C{0:000}", 7);

The result will be: C007

Is there a function in SQL Server that does the same thing? Or do I have to create a function myself?

like image 813
Clive Cauchi Avatar asked Oct 27 '25 21:10

Clive Cauchi


2 Answers

Use FORMATMESSAGE() function.

SELECT FORMATMESSAGE('This is the %s and this is the %s.', 'first variable', 'second variable') AS Result; 

https://msdn.microsoft.com/en-us/library/ms186788.aspx

like image 63
hastrb Avatar answered Oct 29 '25 12:10

hastrb


In straight TSQL, there is nothing as powerful as the C# string.format. The CONVERT() function offers some limited formatting, but for anything it can't handle you'd have to write your own function. Google "SQL String functions" to see what tools you have in your SQL arsenal.

like image 26
Tab Alleman Avatar answered Oct 29 '25 11:10

Tab Alleman