The following code stats that, how many users that has created at least one post on a specific SE site.
DECLARE @RowCount INT
SET @RowCount = (SELECT COUNT(DISTINCT OwnerUserId)
FROM Posts where OwnerUserId is not null)
PRINT FORMATMESSAGE('%s has \r\n %d users', DB_NAME(), @RowCount+1)
which basically works though, the FORMATMESSAGE
part does not work as expected.
Usually, \n
(in lots of systems and languages) and \r\n
(mainly in MS systems) represent new line. I tried both, and even /r/n
, none of them worked.
How can I output a new line with FORMATMESSAGE
in transact sql?
Best Answer
The problem is the interpretation of
\r\n
as carrriage return and line feed. SQL Server does interpret\r\n
like this in some places, but the FORMATMESSAGE function is not one of them, so instead you need to useCHAR(13)
andCHAR(10)
which represents the the ASCII codes for Carriage Return (13) and Line Feed (10):Or use a variable: