我有一个列,列的名字格式如下:“姓,名”
我想使用一个正则表达式,比如:A+,A+,但是我不知道如何在T中这样做。在甲骨文中,我会使用REGEXP_LIKE,Server 2016有类似的地方吗?
我需要这样的东西:
UPDATE table
SET is_correct_format = 'YES'
WHERE REGEXP_LIKE(table.name,'[A-Z]+,[ ]?[A-Z]+');发布于 2017-08-15 18:27:34
首先,区分大小写取决于DB的排序规则,尽管使用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发布于 2017-08-15 18:27:44
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可选的空间很难解决,所以因为它是一个法律字符,所以当它存在的时候,我只是用另一个法律字符来代替它。
TSQL没有在regex中提供*或+的“重复模式”,因此您必须在搜索模式中多次计算字符和构造模式。
我在逗号处拆分字符串,计算前后的alpha,并构建一个匹配的搜索模式。
笨重但可行。
https://stackoverflow.com/questions/45698496
复制相似问题