Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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 2008 R2中的Patindex_Sql_Sql Server 2008_Patindex - Fatal编程技术网

SQL Server 2008 R2中的Patindex

SQL Server 2008 R2中的Patindex,sql,sql-server-2008,patindex,Sql,Sql Server 2008,Patindex,我正在尝试使用SQL Server 2008 R2中的PATINDEX函数从字符串中提取值3 Charged Hourly Fee for 3 CR for BCP202DL Personal Development II 但我似乎犯了一个错误 我试过了 SELECT PatIndex('%[0-9]%', 'Charged Hourly Fee for 3 CR for BCP202DL Personal Development II') 它返回位置24,但我需要值3 是否有人可以帮助解决

我正在尝试使用SQL Server 2008 R2中的
PATINDEX
函数从字符串中提取值3

Charged Hourly Fee for 3 CR for BCP202DL Personal Development II
但我似乎犯了一个错误

我试过了

SELECT PatIndex('%[0-9]%', 'Charged Hourly Fee for 3 CR for BCP202DL Personal Development II')
它返回位置
24
,但我需要值
3

是否有人可以帮助解决此问题?

请尝试:

Select substring(Data, PatIndex('%[0-9]%', Data), 1)
from(
    select 'Charged Hourly Fee for 3 CR for BCP202DL Personal Development II' as Data
)x

如果您必须使用patindex功能,并且您的项目说明中永远不会包含其他数字,如“3”,则可以使用以下数字:

select patindex('%3%', 'Charged Hourly Fee for 3 CR for BCP202DL Personal Development II')
这将返回24小时

我怀疑您看到的是一个字符串,其中订购的数量始终位于同一位置。在这种情况下,我将使用substring函数

declare @str nvarchar (max)
set @str='Charged Hourly Fee for 3 CR for BCP202DL Personal Development II'
Select substring(@str, PatIndex('%[0-9]%', @str), 1)
这将返回字符串中的第一个数字


您当时的做法是返回第一个数字的位置,我的代码将返回该位置的值。

您能否发表自己对该问题的看法?指出您的错误会更容易。请添加预期输出。我尝试了“选择PatIndex(“%[0-9]]”,“BCP202DL Personal Development II收取每小时3 CR费用”),返回24,但我想要值3?
24
是数字的位置
3
。您需要查看
子字符串
,并传递使用
PATINDEX找到的位置。你快到了!谢谢但是,这将返回您提到的位置24,但我想提取值“3”。该值并不总是“3”,但总是出现在该位置。我怎样才能做到这一点?也许我不需要使用patindex?