首页 > 解决方案 > 如何将 Oracle 中的正则表达式查询转换为 SQL Server?

问题描述

我是正则表达式查询的初学者,因此我想问您如何将 Oracle 中的这个正则表达式查询转换为 SQL Server?

select * 
from Sales 
where regexp_like(productname,'^[A-Z]{3}[0-9]+$')

我将其转换为以下查询:

select * 
from Sales 
where substr(productname, 1, 3) in ([A-Z]) 

那是对的吗?

谢谢你。

标签: sql-serverregex

解决方案


您可以使用以下查询:

SELECT *
FROM Sales
WHERE LEFT(productname, 3) LIKE '[A-Z][A-Z][A-Z]' -- three chars ([A-Z]{3})
  AND NOT RIGHT(productname, LEN(productname) - 3) LIKE '%[^0-9]%' -- only numbers after the first three chars
  AND NOT LEN(RIGHT(productname, LEN(productname) - 3)) = 0 -- at least one number

dbfiddle.uk 上的演示


推荐阅读