sql-server - 如何将 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])
那是对的吗?
谢谢你。
解决方案
您可以使用以下查询:
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
推荐阅读
- c# - 在控制器中接收表单数据时出现问题。C# - MVC
- python - 如何使用此特定路径导航到 Selenium 中的站点?
- android - AOSP:在哪里存储工厂数据?
- c# - 自动缩放文本以适应单声道屏幕
- javascript - 无法正确显示结果
- reactjs - 如何将身份验证令牌传递给端点?
- quarkus - 使用 Rancher Rio 部署 Quarkus 应用程序的步骤
- javascript - 测验结束时的休息按钮不起作用
- c# - 在 ASP.NET Core 中重命名 AccountController
- image - 如何检测泡罩包装中的缺陷/缺失药丸?(Matlab)