Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

Maybe you should just be looking for carriage returns if you arent storing the "\n" literal in the field. Something like

  SELECT *
 FROM table
 WHERE column LIKE '%
 %'

or select * from table where column like '%'+char(13)+'%' or column like '%'+char(10)+'%'

(Not sure if char(13) and 10 work for SQLite

UPDATED: Just found someone's solution here They recommend to replace the carriage returns

So if you want to replace them and strip the returns, you could

     update yourtable set yourCol = replace(yourcol, '
     ', ' ');

The following should do it for you

SELECT *
FROM your_table
WHERE your_column LIKE '%' + CHAR(10) + '%'

If you want to test for carriage return use CHAR(13) instead or combine them.

I've found a solution myself. There is few way (with some dedicated function) to convert ascii code to symbol in SQLite at the moment (CHAR function is not support and using '\n' or '\r' directly doesn't work). But we can convert using CAST function and passing in a Hex string (specified by append X or x before the string) in SQLite like this:

-- use CHARINDEX
SELECT * FROM MyTable WHERE CHARINDEX(CAST(x'0A' AS text),MyColumn,0) > 0
-- use LIKE
SELECT * FROM MyTable WHERE MyColumn LIKE '%' || CAST(x'0A' AS text) || '%'

The Hex string '0A' is equal to 10 in ascii code (\r). I've tried with '0D' (13 or '\n') but it won't work. Maybe the \n character is turned to \r after being saved in to SQLite table.

Hope this helps others! Thanks!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top