Computer >> Computer tutorials >  >> Programming >> MySQL

Search text containing a new line in MySQL?


You can use REGEXP. Let us first create a table −

mysql> create table DemoTable
(
   Name varchar(100)
);
Query OK, 0 rows affected (1.61 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John\nSmith');
Query OK, 1 row affected (0.73 sec)
mysql> insert into DemoTable values('John Doe');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('David\nMiller');
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable values('Carol Taylor');
Query OK, 1 row affected (0.27 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+
| Name         |
+--------------+
| John Smith   |
| John Doe     |
| David Miller |
| Carol Taylor |
+--------------+
4 rows in set (0.00 sec)

Here is the query to search text containing a new line in MySQL −

mysql> select *from DemoTable where Name regexp "\n";

This will produce the following output −

+--------------+
| Name         |
+--------------+
| John Smith   |
| David Miller |
+--------------+
2 rows in set (0.41 sec)