Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL Server中使用patindex检查六个连续数值_Sql_Sql Server_Tsql - Fatal编程技术网

在SQL Server中使用patindex检查六个连续数值

在SQL Server中使用patindex检查六个连续数值,sql,sql-server,tsql,Sql,Sql Server,Tsql,我需要使用PATINDEX函数检查字符串中是否有6个连续的数值 括号中的示例显示数字,如果它适合6位数字: case 1 - s24334fth06 case 2 - 234567tgf (234567) case 3 - t443786ftu03 (443786) case 4 - tt43456 预期成果: case 1 - 0 (it has only 5 continuous numeric values, so no index is given) case

我需要使用PATINDEX函数检查字符串中是否有6个连续的数值

括号中的示例显示数字,如果它适合6位数字:

case 1 - s24334fth06 
case 2 - 234567tgf       (234567)
case 3 - t443786ftu03    (443786)
case 4 - tt43456
预期成果:

case 1 - 0   (it has only 5 continuous numeric values, so no index is given)
case 2 - 1   
case 3 - 2  
case 4 - 0
我试过了

PATINDEX('%[0-9]{6}%', @string)
对于所有情况,它都为0

您可以使用:

PATINDEX('%[0-9][0-9][0-9][0-9][0-9][0-9]%', @string)
和提取第一次出现:

SELECT c, 
  SUBSTRING(c,NULLIF(PATINDEX('%[0-9][0-9][0-9][0-9][0-9][0-9]%', c),0), 6) AS r
FROM tab;
您可以使用:

PATINDEX('%[0-9][0-9][0-9][0-9][0-9][0-9]%', @string)
和提取第一次出现:

SELECT c, 
  SUBSTRING(c,NULLIF(PATINDEX('%[0-9][0-9][0-9][0-9][0-9][0-9]%', c),0), 6) AS r
FROM tab;

实际上,您可以在此处使用LIKE运算符:

SELECT *
FROM yourTable
WHERE col LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%';

SQL Server几乎没有内置的正则表达式支持,但他们试图通过使用一些正则表达式功能增强LIKE来部分弥补这一点。

您实际上可以在此处使用LIKE操作符:

SELECT *
FROM yourTable
WHERE col LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9]%';
SQL Server几乎没有内置的正则表达式支持,但他们试图通过使用一些正则表达式功能增强LIKE来部分弥补这一点。

为什么要使用patindex而不是LIKE和case

为什么要使用patindex而不是like和case


下面的答案适用于您,但SQL Server不完全支持正则表达式,因此您的{6}不起作用。下面的答案适用于您,但SQL Server不完全支持正则表达式,因此您的{6}不起作用。我认为Sybase做出了最初的设计选择。@GordonLinoff很高兴知道这一点:-。我认为Sybase做出了最初的设计选择。@GordonLinoff很高兴知道这一点:-