SQL Server try to convert string to int because of Data Type Precedence:
When an operator combines two expressions of different data types, the
rules for data type precedence specify that the data type with the
lower precedence is converted to the data type with the higher
precedence. If the conversion is not a supported implicit conversion,
an error is returned.
INT
is 16 on list of precedece and string constant(VARCHAR
) is lower. So SQL Server try casting every string to INT
.
Cast RecordCount
to string:
DEMO
select
case when @RecordCount = 0
THEN 'There was no data found for tableABC: ' + @Today
ELSE 'tableABC imported ' + CAST(@RecordCount AS NVARCHAR(100)) +' rows for date range ' + @Today
end
also you should consider using CONCAT
for string concatenation instead of +
. Then you don't need to cast:
select
case when @RecordCount = 0
THEN CONCAT('There was no data found for tableABC: ',@Today)
ELSE CONCAT('tableABC imported ', @RecordCount,' rows for date range ', @Today)
end
Another solution is to use FORMATMESSAGE
:
DECLARE @RecordCount as INT = 1;
DECLARE @today as VARCHAR(10) = convert(varchar(10),getdate(),120);
SELECT
CASE
WHEN @RecordCount = 0 THEN FORMATMESSAGE('There was no data found for tableABC: %i', @RecordCount)
ELSE FORMATMESSAGE('tableABC imported %i rows for date range %s', @RecordCount, @today)
END