SQL Server 2016 How to use a simple Regular Expression in T-SQL?

我只是一个虾纸丫 提交于 2019-12-20 06:39:50

问题


I have a column with the name of a person in the following format: "LAST NAME, FIRST NAME"

  • Only Upper Cases Allowed
  • Space after comma optional

I would like to use a regular expression like: [A-Z]+,[ ]?[A-Z]+ but I do not know how to do this in T-SQL. In Oracle, I would use REGEXP_LIKE, is there something similar for SQL Server 2016?

I need something like the following:

UPDATE table 
SET is_correct_format = 'YES'
WHERE REGEXP_LIKE(table.name,'[A-Z]+,[ ]?[A-Z]+');

回答1:


First, case sensitivity depends on the collation of the DB, though with LIKE you can specify case comparisons. With that... here is some Boolean logic to take care of the cases you stated. Though, you may need to add additional clauses if you discover some bogus input.

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



回答2:


The tsql equivalent could look like this. I'm not vouching for the efficiency of this solution.

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

The optional space is hard to solve, so since it's next to a legal character I'm just replacing with another legal character when it's there.

TSQL does not provide the kind of 'repeating pattern' of * or + in regex, so you have to count the characters and construct the pattern that many times in your search pattern.

I split the string at the comma, counted the alphas before and after, and built a search pattern to match.

Clunky, but doable.



来源:https://stackoverflow.com/questions/45698496/sql-server-2016-how-to-use-a-simple-regular-expression-in-t-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!