Search code examples
javasql-serveribatis

How to insert a line break in a SQL Server


I have a java apps.

I insert some text into sql server database

java:

courriel.setTxtCouContenu(corps);

When i inspect my var corps i have this:

enter image description here

You can see \r\n

So when i look in my database there is not line break.

My insert statement (iBatis):

<statement id="insert"
    parameterClass="test.business.bo.TestBO"
    resultClass="java.math.BigDecimal">
    INSERT INTO dbo.E_COUR (ID, CORPS,)
    VALUES (#testBO.id:Numeric#,
    #corps:Varchar#)
    SELECT SCOPE_IDENTITY()
    AS value
</statement>

Solution

  • Is the \r\n still in the string when you use the inserted content or will this vanish?

    Try this in SSMS:

    declare @tbl TABLE(testString VARCHAR(100));
    insert into @tbl VALUES('test with \r\n encoded line break')
                          ,('test with' + CHAR(13) + CHAR(10) + 'windows line break') 
                          ,('test with' + CHAR(10) + 'simple line break');
    

    Now switch to (output to text (ctrl+T) )

    SELECT * FROM @tbl;
    

    The result:

    test with \r\n encoded line break
    
    test with
    windows line break
    
    test with
    simple line break
    

    With "output to datagrid (ctrl+D)" you will not see the line breaks anyway...