Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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
从T-SQL中具有预定义长度的字符串返回数字_Sql_Sql Server_String - Fatal编程技术网

从T-SQL中具有预定义长度的字符串返回数字

从T-SQL中具有预定义长度的字符串返回数字,sql,sql-server,string,Sql,Sql Server,String,我有一个地址字段,比如 '12345 dummycity 14' 'NL - 54321 City' '12 City4 32154' 我只想提取一行中有5个数字字符的子字符串 有人知道如何在T-SQL中提取该字符串吗?这种字符串处理在SQL Server中有点棘手,但是patindex()为您做了很多工作: select left(stuff(str, 1, patindex('%[0-9][0-9][0-9][0-9][0-9]%', str) - 1, ''), 5) from (va

我有一个地址字段,比如

'12345 dummycity 14'
'NL - 54321 City'
'12 City4  32154'
我只想提取一行中有5个数字字符的子字符串


有人知道如何在T-SQL中提取该字符串吗?

这种字符串处理在SQL Server中有点棘手,但是
patindex()
为您做了很多工作:

select left(stuff(str, 1, patindex('%[0-9][0-9][0-9][0-9][0-9]%', str) - 1, ''), 5)
from (values ('NL - 54321 City')) v(str)

太快了,非常感谢你!我走的很好,但没有将我的模式定义为[0-9]5次!