David Shochet
David Shochet

Reputation: 5375

Formatting strings with placeholders in SQL Server like in C#

I need to store generic messages in an SQL Server table, and then get the values and use them within a stored procedure, e.g. "Dear John", where instead of "John" I would like to be able to insert any name obtained within the procedure. In C#, if I stored the text like this: "Dear {0}", I could then easily format it: string.Format(myString, name). Can I do anything like this in SQL Server, other than just REPLACE? I heard something like using CLR in SQL Server, but have no idea whether or how it would work. What is the best way to achieve what I need?

Could you please help?

Thanks.

Upvotes: 2

Views: 3169

Answers (1)

StaWho
StaWho

Reputation: 2488

xp_sprintf may be what you're looking for :

DECLARE @ret_string varchar (255)
EXEC Master.DBO.xp_sprintf @ret_string OUTPUT, '(%s, %s)', 'Hello', 'World'
PRINT @ret_string

Outputs (Hello, World)

Upvotes: 2

Related Questions