sqlh2spring-boot-test

Insert New Line In Spring Boot H2 Test


I try to create a SpringBootTest with h2 database and before that I try to initialize some sql data.

My insert data looks like:

insert into table_name (id, text) values (100, 'New Line\n New line2'); 

integrated like @Sql("insert-path") and inserted successfully but new-line doesn't work in text field. Is there a way to handle it on h2-database or insert script? Any suggestion?

Actual output looks like:

New Line\n New line2

Expected output:

New Line 
New line2

Solution

  • You can do:

    insert into table_name (id, text) values (100, 'New Line
    New line2');
    

    H2 will honor the new line.

    You can also use the U& prefix to insert full Unicode characters. A "new line" is Unicode #10 (aka 000a). The insert could work as:

    insert into table_name (id, text) values (100, U&'New Line\000aNew Line 2');
    

    See H2 String Literals.