将正则表达式转换为SQL

时间:2013-09-24 21:54:40

标签: c# sql regex

我有以下正则表达式:

 ^((\d{4}TS\d{0,4}[A-Z0-9]))$

我需要将其转换为SQL语句,例如

  SELECT DISTINCT field1 FROM WHERE LIKE Mask ORDER BY field2

1 个答案:

答案 0 :(得分:5)

使用MS Access语法:

SELECT DISTINCT field1
FROM Table1
WHERE field1 LIKE '####TS[A-Z0-9]'
OR field1 LIKE '####TS#[A-Z0-9]'
OR field1 LIKE '####TS##[A-Z0-9]'
OR field1 LIKE '####TS###[A-Z0-9]'
OR field1 LIKE '####TS####[A-Z0-9]'
ORDER BY field2

使用SQL Server语法:

SELECT DISTINCT field1
FROM Table1
WHERE field1 LIKE '[0-9][0-9][0-9][0-9]TS[A-Z0-9]'
OR field1 LIKE '[0-9][0-9][0-9][0-9]TS[0-9][A-Z0-9]'
OR field1 LIKE '[0-9][0-9][0-9][0-9]TS[0-9][0-9][A-Z0-9]'
OR field1 LIKE '[0-9][0-9][0-9][0-9]TS[0-9][0-9][0-9][A-Z0-9]'
OR field1 LIKE '[0-9][0-9][0-9][0-9]TS[0-9][0-9][0-9][0-9][A-Z0-9]'
ORDER BY field2

使用Oracle语法:

SELECT DISTINCT field1
FROM Table1
WHERE REGEXP_LIKE(field1, '^[0-9]{4}TS[0-9]{0,4}[A-Z0-9]$')
ORDER BY field2

使用MySQL语法:

SELECT DISTINCT field1
FROM Table1
WHERE field1 REGEXP '^[0-9]{4}TS[0-9]{0,4}[A-Z0-9]$'
ORDER BY field2