简体   繁体   中英

SQL Search & Replace in only ONE Column on ONE Table

I have been building a Magento site and the SKU list got a little erratic with automation. I am currently going through phpMyAdmin and removing " YRS " and " MO " off the end of SKUs instead of opening every product in the admin and saving it through there.

I would like to find a way to automate this. I need to search only one column from one table and replace/erase the " YRS " and " MO " effectively turning " 10-2354-03/06 MO " into " 10-2354-03/06 "

I'm thinking something like "search > catalog_product_entity * sku |for| YRS & MO | replace ||" (i don't know SQL queries so forgive me for such hacked coding)

Capturing the preceding space isn't completely necessary but would be helpful for later on incase something needs to be added on. There are other variables so something that has a "replace XXX with YYY" would be awesome :)

Are there any simple queries I can run to make this happen?

Table is "catalog_product_entity" and column is "sku"

This update statement should do it:

update catalog_product_entity
   set sku = trim(trailing ' YRS' from trim(trailing ' MO' from sku))

If you want to test it in a select statement, run:

select trim(trailing ' YRS' from trim(trailing ' MO' from sku))
from   catalog_product_entity

See sql fiddle test: http://sqlfiddle.com/#!2/699ce/2/0

(removes ' MO' or ' YRS' from the end of the string)

Steven,

If this is MSSQL Server, you should be able to use something like the following:

update YOUR_TABLE_NAME

set catalog_product_entity = replace( catalog_product_entity, ' MO', '' )

from YOUR_TABLE_NAME

Before you run an update statement, try running just a select to compare the original values with your new values. Something like this:

select catalog_product_entity , replace( catalog_product_entity , ' MO', '' ) from YOUR_TABLE_NAME

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM