How to query MySQL for fields containing null characters - mysql

I have a MySQL table with a text column. Some rows have null characters (0x00) as part of this text column (along with other characters).
I am looking for a query that will return all rows containing any null characters for this column, but I cannot figure out how the proper syntax for my "where column like '%...%'" clause.
Thank you!

Right after I submitted the question, a suggested link to this related question provided my answer:
Query MySQL with unicode char code
WHERE column LIKE CONCAT("%", CHAR(0x00 using utf8), "%");

The following worked for me...
WHERE column LIKE "%\0%";

Related

Confusing mysql select results with LIKE and multiple zeros (%00%)

I have created a search query to search a table for a string in multple fields. It works for every string except if the string contains only one or multiple zeros eg: %000% returns rows with 2 or less zeros.
The search fields:
`par_partno` varchar(20) utf8_general_ci
`par_group_id` varchar(16) utf8_bin
`par_desc` text utf8_general_ci
`par_details` text utf8_general_ci
I already tried to cast par_partno to char CAST(par_partno AS CHAR) but the result is the same.
The query:
select `par_id`, `pag_id`, `par_partno`, `par_group_id`,`par_cond`, `par_desc`,`par_price`,
`par_stock`, `par_feature`, `par_weight`, `par_details`, `par_related`, `par_meta`,
`par_picture`, `par_lastmod`
from `parts`
where concat(`par_partno`, `par_group_id`, `par_desc`, `par_details`) like "%000%"
order by `par_group_id` asc
Wrong results:
[
{"par_id":"145100","pag_id":"1","par_partno":"7101263500","par_group_id":"01-00-12","par_cond":"New","par_desc":"Pedal Assy W\/ Booster Mbc, Clutch Cylinder","par_price":"1450.00","par_stock":"1","par_feature":"0","par_weight":"0.1","par_details":"","par_related":null,"par_meta":null,"par_picture":"0","par_lastmod":"2019-01-04 18:14:12"},
{"par_id":"145106","pag_id":"2","par_partno":"7121051100","par_group_id":"01-01-102","par_cond":"New","par_desc":"Seal Intake Valve","par_price":"5.95","par_stock":"1","par_feature":"0","par_weight":"0.1","par_details":"","par_related":"[\"7121040061\"]","par_meta":null,"par_picture":"0","par_lastmod":"2019-01-04 18:14:12"},
{"par_id":"145169","pag_id":"2","par_partno":"7122015100","par_group_id":"01-01-91","par_cond":"New","par_desc":"Engine Gasket Set","par_price":"199.00","par_stock":"1","par_feature":"0","par_weight":"6.4","par_details":"","par_related":null,"par_meta":null,"par_picture":"0","par_lastmod":"2019-01-04 18:14:12"}
]
I might be missunderstanding your question, but isn't the checked string a concatenation of e.g.
par_partno":"7101263500","par_group_id":"01-00-12"
so
710126350001-00-12
which contains a "000" substring.
An improvement to Robin's reference answer:
(too big / codey for comments)
Run only one LIKE query, by combining the searched columns into one CONCATenation.
HAVING CONCAT_WS('-',par_partno,par_group_id,par_descpar_details) LIKE "%000%"
This function combines all checked columns into one string and seperates them with a - (any value that is not part of the LIKE).
This also removes the LOWER() function as it's needless when working with digits.
The hints helped me to resolve this problem.
I removed the concat and replaced it with:
WHERE LOWER(par_partno) LIKE "%000%" OR LOWER(par_group_id) LIKE "%000%"
OR LOWER(par_desc) LIKE "%000%" OR LOWER(par_details) LIKE "%000%"

Select from a field containing spaces using MySQL

I'm attempting to query on a field/column/table in a MySQL DB where the field type is varchar, but some values contains spaces. In my query, I tried to put the exact string to match on in single quotes in a where clause. However, the only rows that are returned are the strings that do not contain spaces.
Here are the values stored in the table/column:
Here is the query and the result that is only returning fields without spaces:
I expected to find a row for "New Business", a row for "Monetary Endorsement", etc. Any idea on how I can modify my query to return the desired fields? Thanks for your help in advance!
Maybe the other values have leading or trailing spaces. You can either use one of the suggestion below:
1.) Use TRIM()
WHERE TRIM(PTD_TRANS_TYPE) = 'NEW BUSINESS'
2.) Use LIKE
WHERE PTD_TRANS_TYPE LIKE '%NEW BUSINESS%'
Here's a Demo.

Filtering special characters in SQL Server

This is question pertaining to SQL Server 2014. I have a table xxx. There is a column col1 of type varchar. The values in this column can have alphanumeric characters like 1A324G. There can also be special characters along with alphanumeric like !2A93C or #AC934D, etc.
There can be any special character (eg: !$#^().-_) in a value for this column. I wanted to extract data with only alphanumeric values and NOT any special characters in it. I was trying to use the LIKE clause with wildcard search pattern but I am not able to weed out the ones with only alphanumeric values.
Can someone please help me and let me know how I can do it?
It's been a while since I've played with sql but something like this should work.
SELECT *
FROM xxx
WHERE col1 NOT LIKE '%!%' OR '%$%';

SQL Select Statement(REGEXP) to find special characters and numbers in an alpha only field

I am using mySQL to query a field which would be LastName. I am looking for any errors in the field such as any special characters or numbers. I am not terribly familiar with SQL so this has been a challenge so far. I have written simple statements with REGEXP but I have run into some issues the REGEXP i was using was:
SELECT LastName FROM `DB`.`PLANNAME` where LastName REGEXP '^([0-9])'
now this turned up results where numbers were the first character in the string and i realized that if anything was in the middle of the string that started with a letter this would not pick it out.
To be clear i just need to find the errors not write a code to clean them out.
Any help would be greatly appreciated
Thanks
Pete
Something like this should do it for you.
SELECT column FROM table WHERE column REGEXP '[^A-Za-z]'
This will return any rows where a character that is not a-z. You might want to add in and '. For O'briens and von lansing etc. Any characters you think are acceptable should go in the character class [], http://www.regular-expressions.info/charclass.html.
Demo: https://regex101.com/r/nC9cG7/1
Maybe you are looking for something like this:
SELECT LastName FROM `DB`.`PLANNAME` WHERE NOT LastName REGEXP '[A-Za-z0-9]';
Here is a documentation on this:
Table 12.9 String Regular Expression Operators

Find non- english characters in mysql

I have a MySQL table which stores email contents as a blob data type. A few of the rows have non-english characters in them. Is there any ways to find only the rows which contain non-english characters?
...
where some_column regexp '.*[^\\w.#].*'
descr LIKE '%[' + CHAR(127)+ '-' +CHAR(255)+']%' COLLATE Latin1_General_100_BIN2
select * from TABLE where COLUMN like '%\0%';
This is working for me on MySQL 5.6 and am happy to use without even thinking how it works. Would be thankful if someone adds an explanation of how this works.