[英]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.
我拥有大约10个SKU,这10个SKU每个都有大约20个子SKU。
Example: _Example - Parent SKU Example7bTL - Child SKU 示例:_Example-父SKU Example7bTL-子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. -最终结果是将所有子SKU都转换为_Example,这样我就可以获得以干净格式出售的单位总数。
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 我正在使用SQL Server 2012
给定您的数据,只是用case
:
select (case when left(sku, 1) = '_' then '_Example' else sku end)
Shouldn't change data just to do a sum(). 不应仅为了做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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.