Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to see spaces in data when selecting with mysql command line client

Tags:

sql

mysql

How can i see spaces in String when using select with the sql-command-line-client?

What i mean is the following. you have three lines. 1, 2 and 3 Spaces. You don't have a chance see the number of spaces.

create table foo(bar varchar(8));
insert into foo values(" "),("  "), ("   ");

select * from foo\g
+------+
| bar  |
+------+
|      |
|      |
|      |
+------+

mysql> select * from foo\G
*************************** 1. row ***************************
bar:  
*************************** 2. row ***************************
bar:   
*************************** 3. row ***************************
bar:    
3 rows in set (0.01 sec)

The only option i came up with is:

mysql> select bar, hex(bar) from foo;
+------+----------+
| bar  | hex(bar) |
+------+----------+
|      | 20       |
|      | 2020     |
|      | 202020   |
+------+----------+
3 rows in set (0.01 sec)

Something like var_export in php would be nice.

like image 696
juwens Avatar asked Mar 12 '13 13:03

juwens


People also ask

Can you use spaces in MySQL?

SPACE() function in MySQL is used to return a string consisting of specified empty space characters.

How do I strip spaces in MySQL?

The TRIM() function returns a string that has unwanted characters removed. Note that to remove the leading spaces from a string, you use the LTRIM() function. And to remove trailing spaces from a string, you use the RTRIM() function.

How do I see the rows in a MySQL table?

The first command you will need to use is the SELECT FROM MySQL statement that has the following syntax: SELECT * FROM table_name; This is a basic MySQL query which will tell the script to select all the records from the table_name table.


1 Answers

It appears there is a string-function QUOTE (Escape the argument for use in an SQL statement) wich works really nice.

-- to add another tricky example
mysql> insert into foo values(" \" ' "), ("''");
Query OK, 1 row affected (0.06 sec)

mysql> select bar, quote(bar) from foo;
+-------+------------+
| bar   | quote(bar) |
+-------+------------+
|       | ' '        |
|       | '  '       |
|       | '   '      |
|  " '  | ' " \' '   |
| ''    | '\'\''     |
+-------+------------+
4 rows in set (0.00 sec)
like image 89
juwens Avatar answered Nov 09 '22 06:11

juwens