SQL: insert string in varchar string

I searched StackOverflow for all possible solutions regarding inserting a string into a SQL text string. I referred to this link, but to no avail. How to insert a line break in a VARCHAR / NVARCHAR SQL Server row

But none of the solutions work for me.

This is what I am trying to do:

insert into sample (dex, col) 
values (2, 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.')

But this is the generated result: (Select Col from the sample, where dex = 2)

This is line 1. This is line 2.

This is the result I want:

This is line 1.
This is line 2.

I use SQL server and SSMS if this helps.

Any ideas why it doesn't work?

+4
source share
4 answers

, . SSMS , , .

, , cntrl + T

enter image description here

, , ( ) enter image description here

+6

CR/LF , , .

, , 2 VARCHAR. CR/LF,

CREATE TABLE sample (dex INT, colnocr VARCHAR(50), col VARCHAR(50)) ;
insert into sample (dex, colnocr, col) values 
(2, 
 'This is line 1.' + 'This is line 2.',
 'This is line 1.' + CHAR(13) + CHAR(10) + 'This is line 2.'
)
;

SELECT * FROM sample

:

| dex |                        colnocr |                              col |
|-----|--------------------------------|----------------------------------|
|   2 | This is line 1.This is line 2. | This is line 1.
This is line 2. |

:

dex     colnocr                                                     col
2       This is line 1.This is line 2.      This is line 1. This is line 2.

: SqlFiddleDemo

+4

:

CREATE TABLE sample(dex INT, col VARCHAR(100));

INSERT INTO sample(dex, col) 
VALUES (2, 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.');

SELECT *
FROM sample;

LiveDemo

:

enter image description here

"" - SSMS , ( ). , , Excel.


SEDE.

LiveDemo-SEDE LiveDemo-SEDE-TextView

:

enter image description here

enter image description here


, :

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

, SSMS 2016 Tools | "" " " / "SQL Server/Results to Grid" " CR/LF ". , , .

+2

All Articles