任何人都知道如何使用 SQL 并替换连字符和屏蔽来做到这一点
Anyone know how to do this with SQL and replacing a hyphen and masking
我要更新很多数据。我有以下问题。
在数据库中有一个 [Name] 字段,其中包含各种格式的名称,他们想要做的是,如果有一个 1 字符后跟一个 - 然后另一个字符将它们折叠在一起,就像这样
AK 咨询有限责任公司 -> AK 咨询有限责任公司
4-T 鲶鱼 -> 4T 鲶鱼
但 L-Tech LLC 将保持不变。
我试过使用
replace(name, '%-%', char(10))
但是留下的字符是字段中的退格键,这不是期望的结果。
从来没有处理过尝试取出连字符然后折叠列中间的那部分。大家有什么想法吗
这是一种方法,我们解析名称,然后根据 _-_
的模式执行条件聚合
全面披露:它可能在大型 table 上表现不佳。
例子
Declare @YourTable Table ([Name] varchar(50))
Insert Into @YourTable Values
('A-K Consulting LLC')
,('4-T Catfish')
,('L-Tech LLC')
Select A.Name
,B.*
From @YourTable A
Cross Apply (
Select S = Stuff((Select ' ' +case when RetVal like '_-_' then replace(RetVal,'-','') else RetVal end
From (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>' + replace((Select replace(A.Name,' ','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
) B1
Order by RetSeq
For XML Path ('')),1,1,'')
) B
Returns
Name S
A-K Consulting LLC AK Consulting LLC
4-T Catfish 4T Catfish
L-Tech LLC L-Tech LLC
EDIT - If Expecting Only 1 Occurrence --- forgot the ltrim/rtrim
Select A.Name
,S = case when patindex('% _-_ %',' '+Name+' ')>0
then ltrim(rtrim(stuff(Name,patindex('% _-_ %',' '+Name+' ')+1,1,'')))
else Name
end
From @YourTable A
EDIT2 - Based on Jeroen's Insight
Select A.Name
,S = ISNULL(STUFF([Name], NULLIF(PATINDEX('% _-_ %', ' ' + [Name] + ' '), 0) + 1, 1, ''), [Name])
From @YourTable A
试试这个:
SELECT [Name],REPLACE([Name],'-',char(10)) FROM Table1;
我要更新很多数据。我有以下问题。
在数据库中有一个 [Name] 字段,其中包含各种格式的名称,他们想要做的是,如果有一个 1 字符后跟一个 - 然后另一个字符将它们折叠在一起,就像这样
AK 咨询有限责任公司 -> AK 咨询有限责任公司
4-T 鲶鱼 -> 4T 鲶鱼
但 L-Tech LLC 将保持不变。
我试过使用
replace(name, '%-%', char(10))
但是留下的字符是字段中的退格键,这不是期望的结果。
从来没有处理过尝试取出连字符然后折叠列中间的那部分。大家有什么想法吗
这是一种方法,我们解析名称,然后根据 _-_
全面披露:它可能在大型 table 上表现不佳。
例子
Declare @YourTable Table ([Name] varchar(50))
Insert Into @YourTable Values
('A-K Consulting LLC')
,('4-T Catfish')
,('L-Tech LLC')
Select A.Name
,B.*
From @YourTable A
Cross Apply (
Select S = Stuff((Select ' ' +case when RetVal like '_-_' then replace(RetVal,'-','') else RetVal end
From (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>' + replace((Select replace(A.Name,' ','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
) B1
Order by RetSeq
For XML Path ('')),1,1,'')
) B
Returns
Name S
A-K Consulting LLC AK Consulting LLC
4-T Catfish 4T Catfish
L-Tech LLC L-Tech LLC
EDIT - If Expecting Only 1 Occurrence --- forgot the ltrim/rtrim
Select A.Name
,S = case when patindex('% _-_ %',' '+Name+' ')>0
then ltrim(rtrim(stuff(Name,patindex('% _-_ %',' '+Name+' ')+1,1,'')))
else Name
end
From @YourTable A
EDIT2 - Based on Jeroen's Insight
Select A.Name
,S = ISNULL(STUFF([Name], NULLIF(PATINDEX('% _-_ %', ' ' + [Name] + ' '), 0) + 1, 1, ''), [Name])
From @YourTable A
试试这个:
SELECT [Name],REPLACE([Name],'-',char(10)) FROM Table1;