3

FORMATMESSAGE function returns NULL when one parameter value using for several msg string.

DECLARE @myvar VARCHAR(10) = 100

SELECT FORMATMESSAGE ('First value %s, Second value %s' , @myvar) AS String

Expected result: First value 100, Second value 100

Actual result: First value 100, Second value null

1 Answer 1

4

You need to specify each parameter:

DECLARE @myvar VARCHAR(10) = 100
SELECT FORMATMESSAGE ('First value %s, Second value %s' , @myvar, @myvar) AS String
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, Kristofer Thank you! But what to do if I will have variable amount of "%s", for example, 70? It is improperly to repeat @myvar 70 times.
Unfortunately you cannot do that with FORMATMESSAGE without adding your variable 70 times. If you want to repeat the same value many times you can use REPLICATE: SELECT REPLICATE('repeat this', 20).
I used REPLACE function - SELECT REPLACE ('First value %s, Second value %s' , '%s', @myvar) AS String

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.