use wildcards with update and replace - mysql

I have a very large MySQL table with lots of data in it, one of the fields is Invoice No, and is a number starting at 1000.001 (This is a string). I have got this from someone that left the company and they imported the data through excel and some of the numbers have come across as 1000.01 instead of 1000.010.
When I run this query in php my admin, it shows there are over 11k rows, so I can see them ok.
SELECT `AnalysisID` , `InvoiceNo`
FROM `STStbl000010`
WHERE `InvoiceNo` LIKE '%.__'
ORDER BY `STStbl000010`.`AnalysisID` ASC
So simply put I need to add a 0 (Zero) to the end of those entries.
I have tried the following, however, it just returns 0 rows effected.
Can I use wildcards like this in and Update and Replace Statement?
UPDATE `STStbl000010AT`
SET `InvoiceNo` = replace(`InvoiceNo`, '%.__', '%.__0')
WHERE `InvoiceNo` LIKE '%.__'
Thanks

You can't use wildcards in a replace, hence no matched rows.
Luckily, if you just want to add a 0, you can concatenate the string values:
UPDATE `STStbl000010AT`
SET `InvoiceNo` = CONCAT(`InvoiceNo`,'0')
WHERE `InvoiceNo` LIKE '%.__'

Related

Add +1 In Front of Each Selected Row of Database

I have a MySQL table containing 10 digit numbers. I need to add +1 in front of each via an UPDATE.
Let's say my SELECT statement looks like this:
SELECT *
FROM num_data
WHERE number REGEXP '^[0-9]{10}$'
How do I add +1 in front of each result of my query above?
Use CONCAT to concatenate strings in MySQL.
UPDATE num_data
SET number = CONCAT('+1', number)
WHERE number REGEXP '^[0-9]{10}$'

mysql update column values with replace

one of my table contains column path stores the URL example:\xyz\attachments, \abc\attachments, etc total i have 16 combinations to replace
i found rows by using rlike in where clause 'abc|xyz|'
have to update xyz with xxx or abc with yyyy
i am not sure how to update these part of the values of column. Is it possible using single query or i have to write 16 queries to do that? please advise here
This is not reliable, but is doable. Basically nested replace() calls:
UPDATE ...
SET yourfield = REPLACE(REPLACE(yourfield, '\\xyz', 'newtext'), '\\abc', 'othertext')
Note that if xyz or abc can appear in multiple places in either string, you may end up replacing something that shouldn't have been.

update specific column of mysql table

I have a quite big table in mysql and I need to change all the records related to this column.
records are like this :
/name/nm0000209/?ref_=ttfc_fc_cl_t1,
/name/nm0000151/?ref_=ttfc_fc_cl_t2,
...,
/name/nm0104594/?ref_=ttfc_fc_cl_t10
what I want is to keep only the string in the middle which is nm0000209, nm0000151,.... I know how to delete specific characters from the right or left of the words by REPLACE or Trim , .., but my problem is that in this case the number of characters in the third part of string are not equal (as you see when it reaches to 10, I have to delete 21 characters from the end instead of 20 characters and since this table contains lots of records I dont know how to do it.
I reaaly appreciate if someone could helop me,
thanks
I want is to keep only the string in the middle which is nm0000209, nm0000151...
You can use 'SUBSTRING_INDEX' on the column to crop part of the column value.
Following example assumes that the said column will have 'name/' as starting pattern.
Example:
update table_name
set column_name = substring_index(
substring_index( column_name, 'name/', -1 )
, '/', 1 );
The same can be used for updating with the same value.
Demo # MySQL Fiddle
One approach would be to use MYSQL's SUBSTRING_INDEX function. It would let you get whatever's after the last slash. Or after the second to last.
For your particular case
select
SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)
from supertext
would yield the desired result
EDIT: for update purposes
UPDATE thetable
SET thefield=SUBSTRING_INDEX(SUBSTRING_INDEX(thefield,'/',-2 ),'/', 1)

Big MySQL table, REPLACE -> very slow query

I have a table with 17.6 million rows in a MyISAM database.
I want to searh an article number in it, but the result can not depend on special chars as dot,comma and others.
I'm using a query like this:
SELECT * FROM `table`
WHERE
replace(replace(replace( replace( `haystack` , ' ', '' ),
'/', '' ), '-', '' ), '.', '' )
LIKE 'needle'
This method is very-very slow. table has an index on haystack, but EXPLAIN shows query can not use that, That means query must scan 17.6 million rows - in 3.8 sec.
Query runs in a page multiple times (10-15x), so the page loads extremly slow.
What should i do? Is it a bad idea to use replace inside the query?
As you do the replace on the actual data in the table, MySQL can't use the index, as it doesn't have any indexed data of the result of the replace which it needs to compare to the needle.
That said, if your replace settings are static, it might be a good idea to denormalize the data and to add a new column like haystack_search which contains the data with all the replaces applied. This column could be filled during an INSERT or UPDATE. An index on this column can then effectively be used.
Note that you probably want to use % in your LIKE query as else it is effectively the same as a normal equal comparison. Now, if you use a searchterm like %needle% (that is with a variable start), MySQL again can't use the index and falls back to a table scan as it only can use the index if it sees a fixed start of the search term, i.e. something like needle%.
So in the end, you might end up having to tune your database engine so that it can held the table in memory. Another alternative with MyISAM tables (or with MySQL 5.6 and up also with InnoDB tables) is to use a fulltext index on your data which again allows rather efficient searching.
It's "bad" to apply functions to the column as it will force a scan of the column.
Perhaps this is a better method:
SELECT list
, of
, relevant
, columns
, only
FROM your_table
WHERE haystack LIKE 'two[ /-.]needles'
In this scenario we are searching for "two needles", where the space between the words could be any of the character within the square brackets i.e. "two needles", "two/needles", "two-needles" or "two.needles".
You could try using LENGTH on the column, not sure if it gives a better affect. Also, when using LIKE you should use the %
SELECT * FROM `table`
WHERE
haystack LIKE 'needle%' AND
LENGTH(haystack) - LENGTH(REPLACE(haystack,'/','')) = 0 AND
LENGTH(haystack) - LENGTH(REPLACE(haystack,'-','')) = 0 AND
LENGTH(haystack) - LENGTH(REPLACE(haystack,'.','')) = 0;
If the haystack is exactly needle then do this
SELECT * FROM `table`
WHERE
haystack='needle';

How to delete the matched value from a column when matched with Char_length

Hi I am using the following sql query to select by length of the company column which works but i am stuck on how to empty/remove those matched column strings. I dont want to delete the row just remove the 1 character the below sql query matched.
select company FROM grocer_append WHERE CHAR_LENGTH(company) = 1
Thanks for your help
If I'm reading right, you just want to remove the character and have company be blank? This would work:
update grocer_append
set company=''
WHERE CHAR_LENGTH(company) = 1;