简体   繁体   English

SQL 删除字符

[英]SQL Removing Characters

I am using the below case statement我正在使用以下案例陈述

***, CASE WHEN Attributes LIKE ',Size = %,' Then  right(Attributes, len(Attributes) - charindex('Size  ', Attributes)) ELSE '' END as Size***

To get the below result得到以下结果

,Size = XXL,Fits to Chest Size = 48 to 50 in,,Closure Type = Snap Button,Material = Cotton,Color = Khaki,Sleeve Length = 33 in,Lining Material = Polyester Thread,,Fabric Weight = 9 oz,,,Resists = Flame,,,,,,,,,,,,,,,,,,,,,,,,,, ,尺寸 = XXL,适合胸围尺寸 = 48 到 50 英寸,,闭合类型 = 按扣,材料 = 棉,颜色 = 卡其色,袖长 = 33 英寸,衬里材料 = 涤纶线,,面料重量 = 9 盎司,, ,抗性=火焰,,,,,,,,,,,,,,,,,,,,,,,,,,,

I want to remove everything and only have Size = XXL我想删除所有东西,只有 Size = XXL

Any help is appreciated.任何帮助表示赞赏。

If this is not an ETL process, then you should really consider changing your data model so you that you do not have to parse your Attribute column every time.如果这不是 ETL 过程,那么您真的应该考虑更改数据 model,这样您就不必每次都解析Attribute列。

Below is a short solution with下面是一个简短的解决方案

Sample data样本数据

create table CsvData
(
  Attribute nvarchar(1000)
);

insert into CsvData (Attribute) values
('Foo = 10,Bar = 20,,,,,Size = XXL ,Fits to Chest Size = 48 to 50 in,,Closure Type = Snap Button ,Material = Cotton ,Color = Khaki ,Sleeve Length = 33 in,Lining Material = Polyester Thread ,,Fabric Weight = 9 oz,,,Resists = Flame ,,,,,,,,,,,,,,,,,,,,,,,,,,');

Solution解决方案

select trim(s.value) as SizeAttribute
from CsvData cd
cross apply string_split(cd.Attribute, ',') s
where left(s.value, 4) = 'Size';

Result结果

SizeAttribute
-------------
Size = XXL

Fiddle to see it in action. 小提琴以查看它的实际效果。

Try This:尝试这个:

DECLARE @CsvData TABLE(Attribute nvarchar(1000));

insert into @CsvData (Attribute) values
('Foo = 10,Bar = 20,,,,,Size = XXL ,Fits to Chest Size = 48 to 50 in,,Closure Type = 
Snap Button ,Material = Cotton ,Color = Khaki ,Sleeve Length = 33 in,Lining Material = 
Polyester Thread ,,Fabric Weight = 9 oz,,,Resists = Flame ,,,,,,,,,,,,,,,,,,,,,,,,,,');


DECLARE @xml_value AS XML,
    @string_value AS VARCHAR(2000),
    @delimiter_value AS VARCHAR(15)
SET @string_value=(SELECT Attribute FROM @CsvData)
SET @delimiter_value =','
SET @xml_value = Cast(( '<studentname>'+ Replace(@string_value, @delimiter_value, '</studentname><studentname>') + '</studentname>' ) AS XML)
SELECT   x.m.query('.').value('.', 'VARCHAR(15)') AS VALUE
FROM   @xml_value.nodes('/studentname') AS x(m)
WHERE  x.m.query('.').value('.', 'VARCHAR(15)')  LIKE 'Size%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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