0

I have used single column to store multiple comments . In that column i have to store every comments in new line so that i can able to differentiate the comments. I have tried CHAR(13) and CHAR(13) + CHAR(10) between two strings . But it's not working.I shown the records in single line.

Tried code:

DECLARE @text NVARCHAR(100)
SET @text = 'This is line 1.' + CHAR(13) + 'This is line 2.'
SELECT @text

Please suggest the solution.

enter image description here

3
  • dbfiddle.uk/… its working Commented Mar 25, 2019 at 4:59
  • It's not working. you tested in which version management studio? Commented Mar 25, 2019 at 5:19
  • @ZaynulAbadinTuhin for your reference i have attached image in query Commented Mar 25, 2019 at 5:20

2 Answers 2

2

You can use PRINT statement instead of SELECT statement to achieve what you want.

For example, you can use any of the followings:

PRINT 'This is line 1.' + CHAR(13) + 'This is line 2.'

Or

PRINT 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'

Or

PRINT CONCAT(N'This is line 1.', 0xd000a, N'This is line 2.')

UPDATE: According to this forum,

You can not see char(13) in SSMS in the Grid format. This character is there and you can see it if you output the result into report, into text, into Excel. But in SSMS grid you can not see this character.

You can change settings from "Results to Grid" to "Results to Text" from menu using the following steps:

Query -> Results to -> Results to Text

Then you will be able to view line break between two strings using any of the followings

SELECT 'This is line 1.' + CHAR(13) + 'This is line 2.'

Or

SELECT 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'

Or

SELECT CONCAT(N'This is line 1.', 0xd000a, N'This is line 2.')
Sign up to request clarification or add additional context in comments.

5 Comments

Yes we can use , But the result we will not get in table format right..
@Gopal I have updated the answer according to your comment. Please check it.
i have one query. Is it possible to view linebreak while Result in Grid format?
According to best of my knowledge, I think it is not possible.
@Gopal, grid format will not show line breaks (which should be CR/LF not just CR or LF in Windows). However, you can preserve those during copy/paste of grid results. From the SSMS menu, Query Options-->Results-->Grid-->Retain CR/LF on copy or save.
0

another way

select concat(N'This is line 1.', 0xd000a, N'This is line 2.')

or

select 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'

3 Comments

@Gopal try 2nd one
@Gopal dbfiddle.uk/… check this link
about code not working in SQL server management studio 2017.. Record stored in single line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.