简体   繁体   中英

SQL Replace with wildcards

I am slowly finding out that the replace string does not work with Wildcards. What I have is approx 10 SKUs, these 10 SKUs each have approx 20 sub SKUs.

Example: _Example - Parent SKU Example7bTL - Child SKU

-End result would be to turn all child SKUs into _Example so i can get a sum of units sold in a clean format.

what i currently have for reference.

use test


CREATE TABLE #test (
Test int,
BillQty char(300) )


select   Quantity, REPLACE (SKU,  '%EXAMP%', 'Example') As Sku
from test.dbo.tblSFCOrderTxn




drop table #Test

Raw Data Example---

Quantity        SKU
210            EXAMPLE7BOTL-C
42             EXAMPLE4BOTL
30             EXAMPLE1BOTL
28             EXAMPLE12BOTL
100            EXAMPLE7BOTL
97             EXAMPLE4BOTL
29             EXAMPLE7BOTL-C

What I want it to be

Quantity        SKU
536             _Example

I am using SQL Server 2012

给定您的数据,只是用case

select (case when left(sku, 1) = '_' then '_Example' else sku end)

Shouldn't change data just to do a sum(). I would go with:

SELELCT   
CASE WHEN SKU LIKE '%EXAMP%' THEN '_Example' ELSE SKU END AS SKU,
SUM(Quantity) AS Quantity
FROM test.dbo.tblSFCOrderTxn
GROUP BY CASE WHEN SKU LIKE '%EXAMP%' THEN '_Example' ELSE SKU END
ORDER BY 1

If you really do want to change the data, it would be:

UPDATE test.dbo.tblSFCOrderTxn
SET SKU = '_Example'
WHERE SKU LIKE '%EXAMP%'   

这会有所帮助

SELECT SUM(Quantity),'_Example' AS SKU FROM test.dbo.tblSFCOrderTxn GROUP BY CASE SKU WHEN '1' THEN '1' ELSE '2' END

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