SQL: Insert a linebreak in varchar string

后端 未结 4 1101
走了就别回头了
走了就别回头了 2021-01-04 08:11

I\'ve searched StackOverflow for all the possible solutions concerning how to insert a linebreak in a SQL text string. I\'ve referred this link but to no avail. How to inser

4条回答
  •  感情败类
    2021-01-04 08:56

    It works perfectly:

    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

    Output:

    The "problem" is SSMS grid view that skips newline characters (and others too). Otherwise you will get different rows height like in Excel.


    You could observe the same behaviour in SEDE.

    LiveDemo-SEDELiveDemo-SEDE-TextView

    Output:


    You could compare it using:

    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.';
    

提交回复
热议问题