在 SQL Server 2008 中通过正则表达式删除特定文本的特定字符
Remove specific characters for a specific text via regex in SQL Server 2008
我正在使用 SQL Server 2008。
数据:
AA00012345/99
AX0000045687044/78
XB000077008/12
我需要做的是只删除开头的连续零(看到除0以外的数字后,允许连续零)和使用 REGEX.
输出字符串中的斜杠 (/) 字符
预期输出:
AA1234599
AX4568704478
XB7700812
我通过这个查询设法做到了:
select dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'prefix') +
dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'beforeslash') +
dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'afterslash')
from table t
不过,我相信应该有更好更专业的方法来处理这个问题。正则表达式的使用是必须的。如有任何建议,我们将不胜感激。
DECLARE @v varchar(100);
SET @V = 'AA00012345/99';
SELECT REPLACE(REPLACE(@v, SUBSTRING(@v, PatIndex('%[0]%', @v), 1), ''), '/', '')
SET @V = 'AX0000045687044/78';
SELECT REPLACE(@v, SUBSTRING(@v, PatIndex('%[0]%', @v), 1), '')
SET @V = 'XB000077008/12';
SELECT REPLACE(@v, SUBSTRING(@v, PatIndex('%[0]%', @v), 1), '')
GO
| (No column name) |
| :--------------- |
| AA1234599 |
| (No column name) |
| :--------------- |
| AX4568744/78 |
| (No column name) |
| :--------------- |
| XB778/12 |
dbfiddle here
这是一种选择。查询将字符串分为两部分,第一部分 - 字母,第二部分 - 其余部分,以数字开头。通过用 space 替换并应用 ltrim
函数
来删除 0
declare @t table (c varchar(100))
insert into @t
values ('AA00012345/99')
,('AX0000045687044/78')
,('XB000077008/12')
select
t.c, q2.p1 + replace(replace(ltrim(replace(q2.p2, '0', ' ')), ' ', '0'), '/', '')
from
@t t
cross apply (select ci = patindex('%[0-9]%', t.c)) q1
cross apply (select p1 = substring(t.c, 1, q1.ci - 1), p2 = substring(t.c, q1.ci, len(t.c))) q2
输出:
AA00012345/99 AA12345/99
AX0000045687044/78 AX45687044/78
XB000077008/12 XB77008/12
我正在使用 SQL Server 2008。
数据:
AA00012345/99
AX0000045687044/78
XB000077008/12
我需要做的是只删除开头的连续零(看到除0以外的数字后,允许连续零)和使用 REGEX.
输出字符串中的斜杠 (/) 字符预期输出:
AA1234599
AX4568704478
XB7700812
我通过这个查询设法做到了:
select dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'prefix') +
dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'beforeslash') +
dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'afterslash')
from table t
不过,我相信应该有更好更专业的方法来处理这个问题。正则表达式的使用是必须的。如有任何建议,我们将不胜感激。
DECLARE @v varchar(100); SET @V = 'AA00012345/99'; SELECT REPLACE(REPLACE(@v, SUBSTRING(@v, PatIndex('%[0]%', @v), 1), ''), '/', '') SET @V = 'AX0000045687044/78'; SELECT REPLACE(@v, SUBSTRING(@v, PatIndex('%[0]%', @v), 1), '') SET @V = 'XB000077008/12'; SELECT REPLACE(@v, SUBSTRING(@v, PatIndex('%[0]%', @v), 1), '') GO
| (No column name) | | :--------------- | | AA1234599 | | (No column name) | | :--------------- | | AX4568744/78 | | (No column name) | | :--------------- | | XB778/12 |
dbfiddle here
这是一种选择。查询将字符串分为两部分,第一部分 - 字母,第二部分 - 其余部分,以数字开头。通过用 space 替换并应用 ltrim
函数
declare @t table (c varchar(100))
insert into @t
values ('AA00012345/99')
,('AX0000045687044/78')
,('XB000077008/12')
select
t.c, q2.p1 + replace(replace(ltrim(replace(q2.p2, '0', ' ')), ' ', '0'), '/', '')
from
@t t
cross apply (select ci = patindex('%[0-9]%', t.c)) q1
cross apply (select p1 = substring(t.c, 1, q1.ci - 1), p2 = substring(t.c, q1.ci, len(t.c))) q2
输出:
AA00012345/99 AA12345/99
AX0000045687044/78 AX45687044/78
XB000077008/12 XB77008/12