sql 无限次重复正则表达式模式
sql repeat regex pattern unlimited times
我需要 select 其中列仅包含数字并以连字符结尾
我正在 运行ning SQL Server Management Studio v17.9.1
我试过:
select * from [table] where [column] like '[0-9]*-'
select * from [table] where [column] like '[0-9]{1,}-'
select * from [table] where [column] like '[0-9]{1,2}-'
none 这些作品。表达式 ([0-9]*-) 在我 运行 反对的任何正则表达式测试器中都有效,SQL 只是不喜欢它,也不喜欢我搜索的其他变体。
您可以过滤除最后一个字符以外的任何字符都不是数字且最后一个字符是破折号的地方。 DATALENGTH/2 假定为 NVARCHAR 类型。如果您使用的是 VARCHAR,只需使用 DATALENGTH
SELECT
*
FROM
[table]
WHERE
[column] like '%-'
AND
LEFT([column], (datalength([column])/2)-1) NOT LIKE '%[^0-9]%'
您可以使用 left()
和 right()
功能,如下所示:
with [table]([column]) as
(
select '1234-' union all
select '{123]' union all
select '1234' union all
select '/1234-' union all
select 'test-' union all
select '1test-' union all
select '700-'
)
select *
from [table]
where left([column],len([column])-1) not like '%[^0-9]%'
and right([column],1)='-';
column
------
1234-
700-
SQL 服务器不支持正则表达式 -- 只是对 like
功能的非常有限的扩展。
一种方法是:
where column like '%-' and
column not like '%[^0-9]%-'
我需要 select 其中列仅包含数字并以连字符结尾
我正在 运行ning SQL Server Management Studio v17.9.1
我试过:
select * from [table] where [column] like '[0-9]*-'
select * from [table] where [column] like '[0-9]{1,}-'
select * from [table] where [column] like '[0-9]{1,2}-'
none 这些作品。表达式 ([0-9]*-) 在我 运行 反对的任何正则表达式测试器中都有效,SQL 只是不喜欢它,也不喜欢我搜索的其他变体。
您可以过滤除最后一个字符以外的任何字符都不是数字且最后一个字符是破折号的地方。 DATALENGTH/2 假定为 NVARCHAR 类型。如果您使用的是 VARCHAR,只需使用 DATALENGTH
SELECT
*
FROM
[table]
WHERE
[column] like '%-'
AND
LEFT([column], (datalength([column])/2)-1) NOT LIKE '%[^0-9]%'
您可以使用 left()
和 right()
功能,如下所示:
with [table]([column]) as
(
select '1234-' union all
select '{123]' union all
select '1234' union all
select '/1234-' union all
select 'test-' union all
select '1test-' union all
select '700-'
)
select *
from [table]
where left([column],len([column])-1) not like '%[^0-9]%'
and right([column],1)='-';
column
------
1234-
700-
SQL 服务器不支持正则表达式 -- 只是对 like
功能的非常有限的扩展。
一种方法是:
where column like '%-' and
column not like '%[^0-9]%-'