Master Station Log
techno-rants, musings, misc geek writing, and occasional caffeine binges
SQL: Make string with FORMATMESSAGE
Date: 2019-01-17 11:08:56
According to Microsoft, this was introduced in SQL 2008. FORMATMESSAGE lets you build a nvarchar string from parameters/variables, which is pretty cool
Format Message builds an NVARCHAR String
Use %s as the placeholders for the string you want to put in
NULL values will come out as (null), which isn't very cool...
DECLARE @BuildMe AS NVARCHAR = '';
DECLARE @Info AS VARCHAR(255) = 'Meep Meep';
DECLARE @More AS CHAR(10) = 'Brown Fox';
DECLARE @Null AS VARCHAR(10) = NULL;
SELECT FORMATMESSAGE('This is the %s and this is the %s. The road runner says %s. And the %s jumps around. With %s'
, 'first variable'
, 'second variable'
, @Null) AS Result;