Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect \n character saved in SQLite table?

I designed a table with a column whose data contains \n character (as the separator, I used this instead of comma or anything else). It must save the \n characters OK because after loading the table into a DataTable object, I can split the values into arrays of string with the separator '\n' like this:

DataTable dt = LoadTable("myTableName");
DataRow dr = dt.Rows[0]; //suppose this row has the data with \n character.
string[] s = dr["myColumn"].ToString().Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries);//This gives result as I expect, e.g an array of 2 or 3 strings depending on what I saved before.

That means '\n' does exist in my table column. But when I tried selecting only rows which contain \n character at myColumn, it gave no rows returned, like this:

--use charindex
SELECT * FROM MyTable WHERE CHARINDEX('\n',MyColumn,0) > 0
--use like
SELECT * FROM MyTable WHERE MyColumn LIKE '%\n%'

I wonder if my queries are wrong?

I've also tested with both '\r\n' and '\r' but the result was the same.

How can I detect if the rows contain '\n' character in my table? This is required to select the rows I want (this is by my design when choosing '\n' as the separator).

Thank you very much in advance!

like image 535
King King Avatar asked Apr 28 '13 20:04

King King


People also ask

How do you identify a new line character in SQL?

Insert SQL carriage return and line feed in a string In SQL Server, we can use the CHAR function with ASCII number code. We can use the following ASCII codes in SQL Server: Char(10) – New Line / Line Break. Char(13) – Carriage Return.

How can I see the structure of a table in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.


1 Answers

Since \n is the ASCII linefeed character try this:

SELECT * 
FROM MyTable 
WHERE MyColumn LIKE '%' || X'0A' || '%'

Sorry this is just a guess; I don't use SQLite myself.

like image 194
BellevueBob Avatar answered Sep 22 '22 15:09

BellevueBob