SQL Server 2016 如何在T-SQL中使用简单的正则表达式?
SQL Server 2016 How to use a simple Regular Expression in T-SQL?
我有一个人名列,格式如下:"LAST NAME, FIRST NAME"
- 只允许大写
- Space逗号后可选
我想使用如下正则表达式:[A-Z]+,[ ]?[A-Z]+ 但我不知道如何在 T-SQL 中执行此操作。在 Oracle 中,我会使用 REGEXP_LIKE,SQL Server 2016 是否有类似的东西?
我需要如下内容:
UPDATE table
SET is_correct_format = 'YES'
WHERE REGEXP_LIKE(table.name,'[A-Z]+,[ ]?[A-Z]+');
首先,区分大小写取决于数据库的排序规则,但使用 LIKE
您可以指定大小写比较。这样......这里有一些布尔逻辑来处理你所说的情况。不过,如果您发现一些虚假输入,您可能需要添加额外的条款。
declare @table table (Person varchar(64), is_correct_format varchar(3) default 'NO')
insert into @table (Person)
values
('LowerCase, Here'),
('CORRECTLY, FORMATTED'),
('CORRECTLY,FORMATTEDTWO'),
('ONLY FIRST UPPER, LowerLast'),
('WEGOT, FormaNUMB3RStted'),
('NoComma Formatted'),
('CORRECTLY, TWOCOMMA, A'),
(',COMMA FIRST'),
('COMMA LAST,'),
('SPACE BEFORE COMMA , GOOD'),
(' SPACE AT BEGINNING, GOOD')
update @table
set is_correct_format = 'YES'
where
Person not like '%[^A-Z, ]%' --check for non characters, excluding comma and spaces
and len(replace(Person,' ','')) = len(replace(replace(Person,' ',''),',','')) + 1 --make sure there is only one comma
and charindex(',',Person) <> 1 --make sure the comma isn't at the beginning
and charindex(',',Person) <> len(Person) --make sure the comma isn't at the end
and substring(Person,charindex(',',Person) - 1,1) <> ' ' --make sure there isn't a space before comma
and left(Person,1) <> ' ' --check preceeding spaces
and UPPER(Person) = Person collate Latin1_General_CS_AS --check collation for CI default (only upper cases)
select * from @table
等效的 tsql 可能如下所示。我不保证此解决方案的效率。
declare @table as table(name varchar(20), is_Correct_format varchar(5))
insert into @table(name) Values
('Smith, Jon')
,('se7en, six')
,('Billy bob')
UPDATE @table
SET is_correct_format = 'YES'
WHERE
replace(name, ', ', ',x')
like (replicate('[a-z]', charindex(',', name) - 1)
+ ','
+ replicate('[a-z]', len(name) - charindex(',', name)) )
select * from @table
可选的 space 很难解决,所以因为它紧挨着一个合法字符,所以我只是用另一个合法字符替换它。
TSQL 没有在正则表达式中提供 'repeating pattern' * 或 + 的类型,因此您必须在搜索模式中计算字符数并构造多次模式。
我在逗号处拆分字符串,计算前后的字母,并构建了一个搜索模式来匹配。
笨拙,但可行。
我有一个人名列,格式如下:"LAST NAME, FIRST NAME"
- 只允许大写
- Space逗号后可选
我想使用如下正则表达式:[A-Z]+,[ ]?[A-Z]+ 但我不知道如何在 T-SQL 中执行此操作。在 Oracle 中,我会使用 REGEXP_LIKE,SQL Server 2016 是否有类似的东西?
我需要如下内容:
UPDATE table
SET is_correct_format = 'YES'
WHERE REGEXP_LIKE(table.name,'[A-Z]+,[ ]?[A-Z]+');
首先,区分大小写取决于数据库的排序规则,但使用 LIKE
您可以指定大小写比较。这样......这里有一些布尔逻辑来处理你所说的情况。不过,如果您发现一些虚假输入,您可能需要添加额外的条款。
declare @table table (Person varchar(64), is_correct_format varchar(3) default 'NO')
insert into @table (Person)
values
('LowerCase, Here'),
('CORRECTLY, FORMATTED'),
('CORRECTLY,FORMATTEDTWO'),
('ONLY FIRST UPPER, LowerLast'),
('WEGOT, FormaNUMB3RStted'),
('NoComma Formatted'),
('CORRECTLY, TWOCOMMA, A'),
(',COMMA FIRST'),
('COMMA LAST,'),
('SPACE BEFORE COMMA , GOOD'),
(' SPACE AT BEGINNING, GOOD')
update @table
set is_correct_format = 'YES'
where
Person not like '%[^A-Z, ]%' --check for non characters, excluding comma and spaces
and len(replace(Person,' ','')) = len(replace(replace(Person,' ',''),',','')) + 1 --make sure there is only one comma
and charindex(',',Person) <> 1 --make sure the comma isn't at the beginning
and charindex(',',Person) <> len(Person) --make sure the comma isn't at the end
and substring(Person,charindex(',',Person) - 1,1) <> ' ' --make sure there isn't a space before comma
and left(Person,1) <> ' ' --check preceeding spaces
and UPPER(Person) = Person collate Latin1_General_CS_AS --check collation for CI default (only upper cases)
select * from @table
等效的 tsql 可能如下所示。我不保证此解决方案的效率。
declare @table as table(name varchar(20), is_Correct_format varchar(5))
insert into @table(name) Values
('Smith, Jon')
,('se7en, six')
,('Billy bob')
UPDATE @table
SET is_correct_format = 'YES'
WHERE
replace(name, ', ', ',x')
like (replicate('[a-z]', charindex(',', name) - 1)
+ ','
+ replicate('[a-z]', len(name) - charindex(',', name)) )
select * from @table
可选的 space 很难解决,所以因为它紧挨着一个合法字符,所以我只是用另一个合法字符替换它。
TSQL 没有在正则表达式中提供 'repeating pattern' * 或 + 的类型,因此您必须在搜索模式中计算字符数并构造多次模式。
我在逗号处拆分字符串,计算前后的字母,并构建了一个搜索模式来匹配。
笨拙,但可行。