I need remove dots and hyphens on a string on compare in Where clause.
This is my query now:
Select * from tbl_sometable where some_column in ('10000000000', '1999999999')
But, the some_column have values like this: '129.012.120-01' and I need filter the values too, like the values in a clause "in".
How I can do this? I using MySQL, I see an example using Translate, but not work in MySQL.
Thanks and best Regards.
WHERE
REPLACE
(
REPLACE
(
some_column, "-", ""
), ".", ""
) in ('10000000000', '1999999999')
WHERE REPLACE(REPLACE(some_column, '.', ''), '-', '') in ('10000000000', '1999999999')
Related
I have a database that contains a column "Code" where the records have the following format "xx-xxx" and "xx-xx", for the later format i want to add a zero after the "-" to make it "xx-0xx", is there anyway to count the characters after a certain pattern in Mysql
Hmmm. If those are your only two possibilities, you can use case:
select (case when length(code) = 5
then replace(code, '-', '-0')
else code
end) as new_code
If you want to be more general, deconstruct the string and build it back again:
select concat_ws('-', substring_index(code, '-', 1),
lpad(substring_index(code, '-', -1), 3, '0')
)
Yes, you can use the CHAR_LENGTH(str) like this:
SELECT code,CHAR_LENGTH(SUBSTR(code,3))
from table
I am writing the mysql query where I need to compare data entered by user with the sku column in database tab
Query is like
SELECT *
FROM `is_product_info`
WHERE REPLACE( '-', '', sku ) LIKE '%ETI2006%'
so that if sku in database contains "-" in sku, I am replacing all hyphens with "" before comparing.
so whether sju no contains ETI2006 or ETI-2006, it will come in output
I think you may just like this
SELECT * FROM is_product_info WHERE REPLACE( sku , '-', '' ) LIKE '%ETI2006%'
I didn't try on a running MySQL but this should work:
SELECT *
FROM `is_product_info`
WHERE sku REGEXP REPLACE('ETI-2006','-','-?');
I made the mistake in replace syntax, it works with the below one:
SELECT * FROM is_product_info WHERE REPLACE( sku , '-', '' ) LIKE '%ETI2006%'
Using replace() on every row is what's slowing it down.
All of these approaches should be faster - see which one works best for you:
Option 1 - use LIKE twice:
WHERE sku LIKE '%ETI-2006%'
OR sku LIKE '%ETI2006%'
Option 2 - Use RLIKE once:
WHERE sku RLIKE 'ETI-?2006'
Got ids stored in DB with Json format like this
'["1454","474","545"]'
I can build list IDs :
SELECT replace
(replace(
replace(
replace('["1454","474","545"]','[','\'')
,']','\'')
,'"','')
,',','\',\'')
mySql returns '1454','474','545'
But when I try to list DB records from this build list of IDs :
SELECT col FROM table WHERE col in (REPLACE
(REPLACE(
REPLACE(
REPLACE('["1454","474","545"]','[','\'')
,']','\'')
,'"','')
,',','\',\''));
mySql says "0 records" even if I add a "SELECT" before the first "REPLACE"
Any help ?
Try below query, you need to add select in your in clause also:
SELECT col FROM table WHERE col in (select REPLACE
(REPLACE(
REPLACE(
REPLACE('["1454","474","545"]','[','\'')
,']','\'')
,'"','')
,',','\',\''));
Alas, you cannot use in with a comma delimited string. It takes a list of elements, but not within a string. So, this works as you expect:
where x in (1,2,3)
This does not work as you expect (although it does work as I expect0;
where x in ('1,2,3')
This looks for one value of x that is the string '1,2,3'.
The solution is to use the MySQL function find_in_set():
SELECT col
FROM table
WHERE find_in_set(col, REPLACE(REPLACE(REPLACE('["1454","474","545"]', '[','\''
), ']', '\''
), '"', ''
), ',', '\',\''
);
To be honest, though, you might be better off with something like:
where '["1454","474","545"]' like concat('%;', col, '&%')
Is there an easy way to replace all the text in a VARCHAR 255 column from "300-21-2" to "300-21-02" with one query?
Thank you.
This is basic SQL
UPDATE tablename
SET columnname = '300-21-02'
WHERE columnname = '300-21-2'
If the pattern is always the same NNN-NN-N then what you need is:
update tablex
set column = concat( substr(column,1,7), lpad(substr(column,8),2,'0') )
see it at fiddle:
http://sqlfiddle.com/#!2/f59fe/1
EDIT As the op showed the pattern
update tablex
set column = CONCAT(
substring_index(col, '-',1), '-',
lpad(substring_index(substring_index(col, '-',-2), '-', 1),2,'0'), '-',
lpad(substring_index(col, '-',-1), 2, '0') )
If you like to convert the first set like 300 to 00300 as your pattern you add the lpad as this: lpad(substring_index(col, '-',1),5,'0')
This should be a lot easier if mysql has support to regex replace, but as it hasnt you have to work with the strings:
from this value: '300-02-1'
from substring_index(col, '-',1) I'm getting: 300
from substring_index(substring_index(col, '-',-2), '-', 1) I'm getting 02 I did this because just put the substring_index(col, '-',2) gave me 300-02 so, i got it from right to left (-2) then i get the first
and substring_index(col, '-',-1) it bring me 1 because it gets the value from right to left
Then I just concatenate it all formatting the ones I want.
I have a lot of rows with values with percentage sign that I would like to remove from a column.
Is there a good SQL query to achieve this?
Use the REPLACE function:
UPDATE YourTable
SET YourColumn = REPLACE(YourColumn, '%', '');
update your_table set your_column = replace(your_column, '%', '')
If your DBMS does NOT have a "replace" function, you will have to use character substitution using several string functions.
Here is an example in Sybase and SQL Server.
UPDATE YourTable
SET YourColumn = stuff(YourColumn, patindex(YourColumn, '%'), 1, NULL)
This code will find the pattern of '%' in YourColumn, then use that position number to replace the character with NULL.