SQL: Insert a linebreak in varchar string

后端 未结 4 1082
走了就别回头了
走了就别回头了 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:37

    The CR/LF chars are there, it's just that in the format of your output, they are being ignored.

    I've created a fiddle to illustrate this, with 2 VARCHAR columns. In the first one I insert the text with no CR/LF, in the second I include them

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

    if you run the query

    SELECT * FROM sample
    

    The result in plain text are:

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

    but if you run it in tabular :

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

    Check it : SqlFiddleDemo

提交回复
热议问题