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?
Use FORMATMESSAGE() function.
SELECT FORMATMESSAGE('This is the %s and this is the %s.', 'first variable', 'second variable') AS Result;
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.
I would use REPLICATE, combined with RIGHT
'C' + RIGHT(REPLICATE('0', <Padding Amount Here>) + CAST(7 AS NVARCHAR(10)), <Padding Amount Here>)
Usage:
'C' + RIGHT(REPLICATE('0', 10) + CAST(7 AS NVARCHAR(10)), 10)
Produces the follow:
C0000000007
FORMATfunction .....