Print raw string in MySQL [duplicate] - mysql

This question already has an answer here:
Escape line breaks in MySQL output
(1 answer)
Closed 3 years ago.
I have a column which contains special characters. I want to print those characters as they are (escaped).
I tried Quote. But that didn't help me.
This is what I want:
select col1 from table;
"Don't\ttype\tlike\this\n\nNot acceptable"

I'm not sure if there is a built in function for this but you could try using replace:
select replace( replace(col1, '\t', '\\t'), '\n', '\\n' ) from table;
Obviously you will need to add more chained replaces for each other escape character you wish to un-escape.
By adding a \ before the escape string, you convert it to a string literal.

Related

erasing 0's from left to right in mysql [duplicate]

This question already has answers here:
mysql: How can I remove character at start or end of field
(5 answers)
Closed 4 years ago.
I have a column in a mysql database table like:
code
0001109A
00002443BD
.....
1233FGV
0PEI223334
i.e. at some rows the code field could have several 0 from left to right and if I find a field like that I need to extract all the 0s. For example, for 00002443BD I need to return 2443BD, for 0PEI223334 I need to return PEI223334, and so on. The number of 0s is not known. Any suggestions?
You are describing a simple TRIM() operation. Other languages and contexts may call this strip(). In context of what you asked:
SELECT TRIM(LEADING '0' FROM `code`) FROM your_table
For completeness, note that:
The default is to trim whitespace from either end, which is the same as BOTH:
SELECT TRIM(' HI ') --> 'HI'
SELECT TRIM(BOTH FROM ' HI ') --> 'HI'
There's TRAILING to trim from the end of the string
SELECT TRIM(TRAILING FROM ' HI ') --> ' HI'
Resources:
Official Documentation (Check out TRIM, and perhaps LTRIM and RTRIM if you only need to remove spaces.)
Handy run-through

MySQL - Search for string contains "_" with LIKE [duplicate]

This question already has answers here:
Mysql Like containing wildcart giving unexpected result
(2 answers)
Closed 5 years ago.
I have a MySQL table and I need to retrieve some fields from it.
I need the string that contains H_ and HP_ Separately using two different query.
But the problem is the underscore is in the wildcard and i am unable to find correct result.
Example Query of HP_
SELECT nc.`logname`, nc.`client_id`, nc.`Customer_Name`, cb.`bwm_day_cir_upload`, cb.`bwm_day_cir_download`
FROM `new_client` nc
LEFT JOIN `client_bandwidth` cb ON cb.`client_id` = `nc`.`client_id`
WHERE nc.`logname` LIKE '%HP_%' AND nc.`Active` = 'y' ORDER BY TRIM(UPPER(nc.`logname`))
As You told it's a wildcard. Then just escape it with '\_'
SELECT nc.`logname`, nc.`client_id`, nc.`Customer_Name`, cb.`bwm_day_cir_upload`, cb.`bwm_day_cir_download`
FROM `new_client` nc
LEFT JOIN `client_bandwidth` cb ON cb.`client_id` = `nc`.`client_id`
WHERE nc.`logname` LIKE '%HP\_%' AND nc.`Active` = 'y' ORDER BY TRIM(UPPER(nc.`logname`))
Documentation to the rescue! Did you read the following page: https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like
Here's an excerpt:
To test for literal instances of a wildcard character, precede it by
the escape character. If you do not specify the ESCAPE character, \ is
assumed.
\% matches one % character.
\_ matches one _ character.

Is there any function in MySQL for Regex Replace? [duplicate]

This question already has answers here:
How to do a regular expression replace in MySQL?
(13 answers)
Closed 6 years ago.
I have a table which needs stores some name and i need to replace a few characters before comparing them with another string
For instance, My table data is
abc
ghi:dki
ioe dsa
i read a string from user, which is of the form abc, ghi-dki, ioe-dsa. ie, all blankspaces, multiples spaces and symbols are converted to a hyphon(-). Now i need to compare. something like
SELECT MYCOLUMN FROM MYTABLE WHERE {Converted MYCOLUMN} = 'ghi-dki'
Can someone help me for figuring out which MySQL function can do it?
You can't do a regex replace in MySQL, but you can do a match.
SELECT mycolumn FROM tablename WHERE mycolumn REGEXP Replace('ghi-dki', '-', '^[\s:_-]*$');
Note: I didn't completely fill out the symbols character set, you'll have to add whatever you're using.

Trimming whitespace in mysql [duplicate]

This question already has answers here:
MySQL Query, remove all spaces
(2 answers)
Closed 9 years ago.
I have tried using the TRIM() in MySQL to delete all of the whitespace in every field in a column but it will only remove it from the front. I have long strings that have spaces in the middle next to hyphens and I need the space between the hyphens removed. Is there a way to do this?
UPDATE FOO set FIELD2 = TRIM(FIELD2);
Use the REPLACE() method, which can replace every occurrence of a character by another:
UPDATE foo SET field2 = REPLACE(field2, ' ', '');
This query will remove every spaces in the field2 column.
Definition of the method, from the MySQL doc:
Returns the string str with all occurrences of the string *from_str* replaced by the string *to_str*. REPLACE() performs a case-sensitive match when searching for *from_str*.
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
-> 'WwWwWw.mysql.com'
This function is multi-byte safe.

mySql - find non-Ascii character in html [duplicate]

This question already has answers here:
How can I find non-ASCII characters in MySQL?
(10 answers)
Closed 9 years ago.
I'm trying to find all non-asci characters I have in my DB in a specific table and column. In that column are stored Html description, and in some of them I've exotic or non-existing characters (for example: Hà¶ganà¤s ).
I'm triyng to match them with this query:
SELECT * FROM project_version WHERE description REGEXP '[^()\x00-\xFF\,\.-\<\>="\' /:;&=]'
But I think I'm missing something, cause it returns all of my records. Does anyone any advice?
Thanks in advance
Try moving hyphen to start or end otherwise it needs to be escaped also ^ will be treated as literal ^ in character class:
SELECT * FROM project_version WHERE description REGEXP '[()\x00-\x7F,.<>="\' /:;&=-]'