简体   繁体   中英

How to replace all occurrences of a string in SQL

I have a data as follows

Names
0002_○●○; B0024_○●;
0032_●○; 0041_○●○; D0030_○●;
022_○●●; A0071_○●; 0080_●●○; C0150_○○○;

○● is a non-English name

I hope to get the following result

○●○; ○●; 
●○; ○●○; ○●; 
○●●; ○●; ●●○; ○○○; 

I try to use replace to replace English, numbers, symbols

select REPLACE(Names, '%[^A-Za-z0-9]%', '')
from test

But it didn't work

How can I modify it?

Thank you

If you use SQL Server 2017+, a combination of TRANSLATE() and REPLACE() is an option ( ... probably not the best one):

DECLARE @pattern nvarchar(max) = N'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
SELECT 
   REPLACE(
      TRANSLATE(Names, @pattern, REPLICATE(N'A', LEN(@pattern))), 
      N'A', 
      N''
   ) AS Names
FROM (VALUES
   (N'0002_○●○; B0024_○●;'),
   (N'0032_●○; 0041_○●○; D0030_○●;'),
   (N'0022_○●●; A0071_○●; 0080_●●○; C0150_○○○;')
) test (Names)

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