I have below code:-
DECLARE @NewLineChar AS CHAR(23) = CHAR(13) + CHAR(10)
select \'abc\'+@NewLineChar+\'a\'
I expect the result to be:-
<To test your output, if using SSMS make sure the option Retain CR/LF on copy or save is checked, else all pasted results will loose the line feed. You find this at settings, query results, sql server, results to grid.
Use below query for new line :
DECLARE @strPrint VARCHAR(100);
SET @strPrint = 'abc';
SET @strPrint = @strPrint + CHAR(13);
SET @strPrint = @strPrint + 'a';
SELECT @strPrint;
Edit :
Use PRINT for see new line
PRINT @strPrint;
It will work. But you have to see the result in Result to text
output
select 'abc'+ CHAR(13) +'a'
When using the results to grid option, SSMS use the standard Windows grid control to display the results. This grid control treats each cell value as a plain text and hence the new line characters are ignored.
However if you use the results to text option or the results to file the new line characters are retained.
Refer : https://connect.microsoft.com/SQLServer/feedback/details/381955/sql-server-management-studio-should-show-new-lines-in-records
Enable Result to text
by pressing ctrl + t
then run
DECLARE @NewLineChar AS VARCHAR(2)
SET @NewLineChar = CHAR(13) + CHAR(10)
PRINT 'abc'+@NewLineChar+'a'
U have to concat string with CHAR(13)
This should work:
DECLARE @NewLineChar NVARCHAR(200);
SET @NewLineChar = 'abc' + CHAR(13) + 'a';
PRINT @NewLineChar;
OUTPUT:
abc
a
Try using PRINT
and not SELECT
to display the result.
DECLARE @NewLineChar AS VARCHAR(23) = CHAR(13) + CHAR(10)
PRINT 'abc'+@NewLineChar+'a'
(Don't forget to move to the Messages tab to view the result)
The SSMS grid display does not display line breaks or newline characters.