Sql-server – How to escape ‘%’ inside varchar @params for RAISERROR funcion in SQL Server 2012

raiserrorsql servert-sql

How I should escape the % character in params so my RAISERROR retun my message

declare @msg varchar(max)  = (SELECT ' Message with %  ' AS MSG)

if @msg is not null
begin
    RAISERROR (@msg ,16,1); 
end

This will rise error with message

Msg 2787, Level 16, State 1, Line 4
Invalid format specification: '% '.

For end users this message is unreadable.

My messages is generated from database and setting this message.

Way I do avoid getting wrong message is replacing

set @msg = REPLACE(@msg,'%','P')

But I still do not know how to add % sign

Best Answer

Use %%:

raiserror(N'This is a message with %%', 0, 1);