I have spent several hours to solve this problem, but I can't solve it.
One of column contain value like bellow, and the value contain some blank spaces.
光学传感器 - 光电二极管
I want select all the value equal to "光学传感器 - 光电二极管". But the blank space may be ASCII encoding('\x0a'), or utf8 encoding('\xa0'), So when I execute SQL string LIKE this, will return None.
SELECT *
FROM `icbase_icattrvalue`
WHERE `value` LIKE '光学传感器 - 光电二极管'
I have try SQL like this:
SELECT *
FROM `icbase_icattrvalue`
WHERE REPLACE( `value` , '\xa0', ' ' ) LIKE REPLACE( '光学传感器 - 光电二极管', '\xa0', ' ' )
LIMIT 0 , 30
or
SELECT *
FROM `icbase_icattrvalue`
WHERE REPLACE(`value`, CONVERT(char(160) USING utf8), ' ')
LIKE REPLACE('光学传感器 - 电二极管', CONVERT(char(160) USING utf8), ' ')
LIMIT 0 , 30
or some similar SQL string, But they can't help, they all return None.
What should I to , to select value equal to a string, but ignore such space encoding issue.
I'm sorry for my poor English.
As Alexander Gelbukh says in comment i also think that this is due to the incorrect spacing in ur code..
SELECT *
FROM `icbase_icattrvalue`
WHERE REPLACE( `value` , '\xa0', ' ' ) LIKE REPLACE( '光学传感器 - 光电二极管', '\xa0', ' ' )
LIMIT 0 , 30
Actually this is the right answer, I should replace c2a0, not a0.
NON BREAK SPACE in utf-8 is c2a0
And in python unicode is a0
I get confused with it, Thanks everybody.
SELECT * FROM `icbase_icattrvalue`
WHERE id = 197193 and
REPLACE( `value` , UNHEX('c2a0'), ' ' ) = REPLACE( '光学传感器 - 光电二极管', UNHEX('c2a0'), ' ' );
Related
I want to get nth word from a column I'm using a code line and it works for me but there is an issue, for example:
First line is: "the Nth word from database"
Second line is: "return the Nth word from database and more words"
When I search for 6th word 'database' it returns my first line and second line but I don't want to get my first line because it has only 5 words.
thank you all
My code line:
SELECT *,
SUBSTRING_INDEX(SUBSTRING_INDEX(`Text`, ' ', 6), ' ', -1) as Nth
FROM `tbl_name`
Having six words in you sentence means that you have to have at least five spaces, adding simlpe condition will resolve your problem:
select *,
case when length(`text`) - length(replace(`text`, ' ', '')) >= 5 then
substring_index(replace(`text`, substring_index(`text`, ' ', 5) , ''), ' ', 2)
else null end Nth
from `tbl_name`
Also I changed your query, because it didn't take into account that you might not have 6th space (exactly six words).
Demo
Or even more concicse:
select *,
substring_index(substring_index(`text`, ' ', 5 - (length(`text`) - length(replace(sentence`text` ' ', ''))) - 1), ' ', 1)
from `tbl_name`
Another demo.
You should update your query with where clause, in where you can count the number of words by the following query.
SELECT *, SUBSTRING_INDEX(SUBSTRING_INDEX(`Text`, ' ', 6), ' ', -1) as Nth
FROM `tbl_name`
where (COUNT(column1) - LENGTH(replace(column1, ' ', '')) > 5
You should have to take count of spaces or whichever string which you want to take & then need to apply having-clause on that count.
SELECT
* ,
SUBSTRING_INDEX( SUBSTRING_INDEX( `text` , ' ', 6 ) , ' ', -1 ) AS Nth,
ROUND( ( LENGTH( `text` ) - LENGTH( REPLACE( `text` , " ", "" ) ) ) / LENGTH( " " ) ) AS countq
FROM `xp_test`
HAVING
countq >= 5
I have a vendors table in my database that am experimenting with, as shown below
And when i run the sql command below
SELECT vendor_name
FROM vendors
ORDER BY vendor_name
LIMIT 10
I get the output below
My issue is am trying to extract the second word from each vendor_name and when the second word doesn't exist it's supposed to return a blank cell.
And below is the sql query i have written to do just that
SELECT vendor_name,
SUBSTRING(
SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1),
1,
LOCATE( ' ', SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1) ) - 1
) AS second_word
FROM vendors
ORDER BY vendor_name
LIMIT 10
And here is the output of that sql query
If you notice from the output above, when the words in the vendor_name are more than two, it returns the second word just fine and when the vendor_name contains one word it returns a blank cell as expected.
Problem comes when the vendor_name contains exactly two words, instead of returning the second word it is returning a blank cell for example in the case of American Express and ASC Signs.
How can i better improve my query so that even when the vendor_name does contain two words, it does return the second word instead of a blank cell?
Thank you.
That's because there is no space after the second word, if the text ends there, the locate() has no space to find.
Quick hack: Add a space at the end.
LOCATE( ' ', CONCAT(SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1), ' ') ) - 1
SELECT vendor_name , substr(vendor_name , instr(vendor_name, " ") ,
case when LOCATE (' ', vendor_name,instr(vendor_name, " ") ) > 0 then LOCATE (' ',
vendor_name,instr(vendor_name, " ") ) else CHAR_LENGTH (vendor_name) end )
from vendors ;
I took tips from both #stick bit and #kiran gadhe and i came up with this sql query and it's working just fine
SELECT vendor_name,
CASE
WHEN INSTR( vendor_name, ' ' ) = 0
THEN
''
ELSE
SUBSTRING(
SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1),
1,
LOCATE( ' ', CONCAT(SUBSTRING( vendor_name, LOCATE(' ', vendor_name) + 1), ' ') ) - 1
)
END AS second_word
FROM vendors
ORDER BY vendor_name
LIMIT 10
this is my first question here, having a strange problem with MySQL:
I have phone numbers in a database field 'phone' of type varchar(50) utf8mb4_unicode_ci. Type is varchar, because the numbers can have delimiters like / or - and spaces for formatting.
If I select the phone number with:
SELECT phone, REPLACE(REPLACE(REPLACE(REPLACE(phone,' ',''),'-',''),'/',''),'.','') FROM [table] WHERE id=405760
the output is:
0151 / 434 77 588, 015143477588
So the REPLACE part does work, also, when I select the
But searching after those phone numbers with the LIKE operator fails for this specific phone number 015143477588 (others work perfectly):
SELECT *
FROM [table]
WHERE REPLACE(REPLACE(REPLACE(REPLACE(phone,' ',''),'-',''),'/',''),'.','') like '%015143477588%'
If I use the = instead LIKE it works.
SELECT *
FROM [table]
WHERE REPLACE(REPLACE(REPLACE(REPLACE(phone,' ',''),'-',''),'/',''),'.','') = '015143477588'
as said, other phone numbers work....any ideas?
I have checked your query in my database it is working fine
SELECT *
FROM users
WHERE REPLACE( REPLACE( REPLACE( REPLACE( phone, ' ', '' ) , '-', '' )
, '/', '' ) , '.', '' )
LIKE '%015143477588%'
You can check your query after change Collation from utf8mb4_unicode_ci to utf8_swedish_ci
Please take a look at a table below:
I need "the last part" of "what I have" to be number if it's all number and string if it contains character.
In Excel I've achieved this with the following function (as shown above):
=IFERROR(VALUE(TEXT(D2;"0"));TEXT(D2;"0"))
However I want to do this in mySQL in order to compute more effectively.
I've "floated" somwhere around CASE with CAST or CONVERT and also TRIM functions, but I haven't been able to put up something sensical.
A nice "bonus" would be to extract "the part part" by looking for the last "" character (so first "" from right of the string) but no idea at all how to achieve that.
Use SUBSTRING_INDEX.
Query
SELECT
CASE
WHEN SUBSTRING_INDEX(records.data, '_', -1) > 1 # is int check '00004949' returns 1
THEN SUBSTRING_INDEX(records.data, '_', -1) + 0 # converts '00004949' to 4949
ELSE SUBSTRING_INDEX(records.data, '_', -1)
END
AS word
FROM (
SELECT "TRA_PL_NWL_EMA_NWLY_DAI_000_20170610_IN1_01P002bc" AS DATA
UNION
SELECT "TRA_PL_NWL_EMA_NWLY_DAI_000_2017_0909_JET_00004949" AS DATA
) records
Result
word
----------
01P002bc
4949
Following query will somewhat achieve the task:
SELECT
case
when SUBSTRING_INDEX(value, "_", -1) REGEXP('(^[0-9]+$)')
then Trim(Leading 0 from SUBSTRING_INDEX(value, "_", -1))
else SUBSTRING_INDEX(value, "_", -1)
end as Value
From yourtable;
Click here for Demo
Hope it helps!
I hope this is what u want.
At least it does what you have asked for.
SELECT
CASE
WHEN (
CONVERT(
substring(
txt,
LENGTH(txt) - LOCATE('_', REVERSE(txt))+2,
length(txt)
)
, signed integer
)
) = 0
THEN substring(
txt,
LENGTH(txt) - LOCATE('_', REVERSE(txt))+2,
length(txt)
)
ELSE CONVERT(
substring(
txt,
LENGTH(txt) - LOCATE('_', REVERSE(txt))+2,
length(txt)
)
, signed integer
)
END as NUMBER
from test.test
This is my test Table and result of SQL:
txt NUMBER
DA_DA_ADAD_ADAD_ADAD_asd123 asd123
DA_DA_ADAD_ADAD_ADAD_000123 123
DA_DA_ADAD_ADAD_ADAD_444 444
DA_DA_ADAD_ADAD_ADAD_bsd123 bsd123
DA_DA_ADAD_ADAD_ADAD_csd123 csd123
DA_DA_ADAD_ADAD_ADAD_dsd123 dsd123
I have a column in MySql that contains a description of a products. Sometimes the description contain more the one space between words and I would like to turn it into one space so I've found it with this query:
SELECT * FROM `database`.`PRODUCTS`
WHERE `PRODUCTS`.`description` LIKE '% %'
and then repaired it by:
UPDATE `database`.`PRODUCTS`
SET `PRODUCTS`.`description` = REPLACE(`PRODUCTS`.`description`,' ',' ')
But it doesn't remove all the double spaces! There are some kind of "special" spaces with (I suspect) different ascii code - 0xA0,0xC2
How can I SELECT it and remove it?
Thanks
These are not 'special' spaces - they are not ascii codes - if they are in your database then you've got bugs in the code which put them there.
Why not just replace them?
....
SET PRODUCTS.description = REPLACE(
REPLACE(
REPLACE(PRODUCTS.description
,CHAR(160),' ')
, CHAR(194), ' ')
, ' ', ' ');