Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Line character \n in SQLite concatenate

Tags:

I know that, in some Programming language, if you do something like:

'This is on first line \n This is on second line' 

Then it will display properly like this:

This is on first line This is on second line 

When I concatenate a string in a SQLite database

SELECT *, [FIELD1] || '\n'  || [FIELD2] from TABLE 

(where [FIELD1] = This is on first line [FIELD2] = This is on second line)

it displays as such:

This is on first line \n This is on second line 

Is there a reason that it isn't displaying the \n characters properly?

like image 619
Janka Avatar asked May 29 '14 10:05

Janka


People also ask

Is it n or \n for New Line?

Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.

How can we concatenate string in SQLite?

The SQL standard provides the CONCAT() function to concatenate two strings into a single string. SQLite, however, does not support the CONCAT() function. Instead, it uses the concatenate operator ( || ) to join two strings into one.

Is there varchar in SQLite?

SQLite does not enforce the length of a VARCHAR. You can declare a VARCHAR(10) and SQLite will be happy to store a 500-million character string there. And it will keep all 500-million characters intact. Your content is never truncated.


2 Answers

SQL has no backslash escapes.

You can generate a newline in a string by writing it directly in the query:

SELECT [Field1] || ' ' || [Field2] FROM MyTable 

or use a blob literal:

SELECT [Field1] || x'0a' || [Field2] FROM MyTable 

or use the char function:

SELECT [Field1] || char(10) || [Field2] FROM MyTable 
like image 59
CL. Avatar answered Oct 15 '22 03:10

CL.


Try char(13) (if you want to see it in Windows notepad).

select col || char(13) from mytable 
like image 38
live-love Avatar answered Oct 15 '22 03:10

live-love