MySQL query to sort column values and ignoring quotes on one of the values


To ignore quotes while ordering column values, use ORDER BY TRIM().

Let us first create a table −

mysql> create table DemoTable784 (
   Message varchar(100)
);
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable784 values('Good');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable784 values('\"This is not a Message\"');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable784 values('Bye');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable784 values('Hello');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable784;

This will produce the following output -

+-------------------------+
| Message                 |
+-------------------------+
| Good                    |
| "This is not a Message" |
| Bye                     |
| Hello                   |
+-------------------------+
4 rows in set (0.00 sec)

Following is the query to sort column values ignoring quotes −

mysql> select *from DemoTable784 order by trim(both '"' from Message);

This will produce the following output -

+-------------------------+
| Message                 |
+-------------------------+
| Bye                     |
| Good                    |
| Hello                   |
| "This is not a Message" |
+-------------------------+
4 rows in set (0.00 sec)

Updated on: 09-Sep-2019

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements