MySQL select char(0x542d01 using ?) smile issue - mysql

From the command
select char(0x542d01);
I expect
T-1
But MySQL returns
T-:)
From a little of google research I found the result can be modified by specifying the charset in the command, something like
select char(0x542d01 using utf8);
But I wasn't able to find a way to read T-1 from 0x542d01. Would some one give me a hand here, please?
More generally, I think I have a charset issue here.

Function convert can be used to convert data between different charsets.
Reference:
http://dev.mysql.com/doc/refman/5.7/en/charset-convert.html
Here is an example of your data:
mysql> select convert(0x542d01 using utf8);
+------------------------------+
| convert(0x542d01 using utf8) |
+------------------------------+
| T- |
+------------------------------+
1 row in set (0.00 sec)

Related

SQL query for detect columns with special characters?

here and in a lot of other websites I have find a lot of posts regarding this question but for some strange reason no one works.
I have a Wordpress database and I need to find all the terms contained in wp_terms that have any special character.
In fact I need to find all that contains anything else of number or letter.
Why this doesn't work? The MySQL query return 0 results.
SELECT * FROM wp_terms WHERE name LIKE '%[^0-9a-zA-Z ]%'
You can use REGEXP to find out this. Also the ^must be outside from [].
SELECT * FROM wp_terms WHERE name REGEXP '[^0-9a-zA-Z ]'
Test
MariaDB [(none)]> SELECT "Hello" REGEXP '[^0-9a-zA-Z ]' as resut;
+-------+
| resut |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> SELECT "-Hello" REGEXP '[^0-9a-zA-Z ]' as resut;
+-------+
| resut |
+-------+
| 0 |
+-------+
1 row in set (0.00 sec)
It does not work because MySQL supports the ANSI version of LIKE. The form that you are using is an extended form associated with SQL Server.
On the other hand, MySQL supports regular expressions which are much more powerful. The regular expression for what you want is:
WHERE name REGEXP '[^0-9a-zA-Z ]'
Note that regular expressions match the pattern anywhere in the string, so you do not need wildcards at the beginning and the end.

Trying to get mysql like function to work with function

The following statement works fine:
mysql> select concat('/Parent/Child/',date_format(subdate(current_date,1),'%Y/%m/%d'));
+--------------------------------------------------------------------------+
| concat('/Parent/Child/',date_format(subdate(current_date,1),'%Y/%m/%d')) |
+--------------------------------------------------------------------------+
| /Parent/Child/2017/06/14 |
+--------------------------------------------------------------------------+
1 row in set (0.00 sec)
but for some reason I can't do a like comparison with this information. What am I missing here?
I keep getting an empty set which I know is incorrect.
Can someone provide me the correct 'where like ' syntax?
It was actually as easy as concatinating % to before and after initial concat.

MySQL table file names encoding

Could someone know what encoding is used here #T0#g0#x0#y0#w0#u0#p0#q0#o0.MYD ?
This is a file name corresponding to table name which name using cyrillic letters.
This is the MySQL internal filename encoding Documented here.
You can convert this back to normal utf8 by using a procedure like:
mysql> SELECT CONVERT(_filename'#T0#g0#x0#y0#w0#u0#p0#q0#o0' USING utf8);
+------------------------------------------------------------+
| CONVERT(_filename'#T0#g0#x0#y0#w0#u0#p0#q0#o0' USING utf8) |
+------------------------------------------------------------+
| Настройки |
+------------------------------------------------------------+
1 row in set (0.00 sec)

Broken unicode after simple concat + left mysql commands

I found some very strange mysql behavior.
If I run the following command:
mysql> select left(concat("A", "B®"), 3);
Then the output is as expected:
+-----------------------------+
| left(concat("A", "B®"), 3) |
+-----------------------------+
| AB® |
+-----------------------------+
1 row in set (0.00 sec)
However, if I change "A" with some number (1 in this case):
mysql> select left(concat(1, "B®"), 3);
The unicode character "®" becomes corrupted:
+---------------------------+
| left(concat(1, "B®"), 3) |
+---------------------------+
| 1B? |
+---------------------------+
1 row in set (0.00 sec)
Anybody knows how to explain this strange behavior and how to avoid it?
The example above is only a reproduction, in the real life it's a concat of numbers together with strings unknown ahead (not hard-coded strings).
Thanks a lot!
Mysql doesn't convert integer to strings literally. It converts number into the binary representation of it, which is not the same. "if the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent binary string form; if you want to avoid that, you can use an explicit type cast, as in this example:
SELECT CONCAT(CAST(int_col AS CHAR), char_col);
Refer this for details.
I would also like to read from others if someone has different opinion.

About mysql regex,how do I search and return string use mysql regex

My table filed's value is "<script type="text/javascript"src="http://localhost:8080/db/widget/10217EN/F"></script>",
I want to analyse this string and fetch the id 10217,how to do use mysql regex?
I know python regex group function can return the id 10217,but i'm not familiar with mysql regex.
Please help me,Thank you very much.
MySQL regular expressions do not support subpattern extraction. You will probably have better luck iterating over all of the rows in your database and storing the results in a new column.
As far as I know, you can't use MySQL's REGEXP for substring retrieval; it is designed for use in WHERE clauses and is limited to returning 0 or 1 to indicate failure or success at a match.
Since your pattern is pretty well defined, you can probably retrieve the id with a query that uses SUBSTR and LOCATE. It will be a bit of a mess since SUBSTR wants the start index and the length of the substring (it would be easier if it took the end index). Perhaps you could use TRIM to chop off the unwanted trailing part.
This query get the Id from the field
SELECT substring_index(SUBSTRING_INDEX(testvar,'/',-3),'EN',1) from testtab;
where as testtab - is table name , testvar - is field name
inner substring get string starts with last 3 / which is
mysql> SELECT SUBSTRING_INDEX(testvar,'/',-3) from testtab;
+----------------------------+
| SUBSTRING_INDEX(testvar,'/',-3) |
+----------------------------+
| 10217EN/F"> |
| 10222EN/F"> |
+----------------------------+
2 rows in set (0.00 sec)
outer substring get
mysql> SELECT substring_index(SUBSTRING_INDEX(testvar,'/',-3),'EN',1) from testtab;
+----------------------------------------------------+
| substring_index(SUBSTRING_INDEX(testvar,'/',-3),'EN',1) |
+----------------------------------------------------+
| 10217 |
| 10222 |
+----------------------------------------------------+
2 rows in set (0.00 sec)