SQL Server 2005 函数提取一组字符串后的子字符串匹配 [英] SQL Server 2005 function to extract a substring match following a set string

查看:50
本文介绍了SQL Server 2005 函数提取一组字符串后的子字符串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有一个很好的紧凑而高效的 SQL Server 函数,它会在给定字符串的第一个匹配项之后返回第一个字符串(以空格结尾).

我有一些代码,但它真的很丑,而且可能很慢.

例如,在 In test 12545 file:x12545.jpg 看起来不错 给定 file: 将返回 x12345.jpg

谢谢.

解决方案

create function dbo.extractAfter(@full nvarchar(max), @part nvarchar(max))返回 nvarchar(max)with 在 null 输入上返回 null作为开始return ltrim(stuff(left(@full,charindex(' ', @full + ' ', charindex(@part,@full)+1)),1、charindex(@part,@full)+datalength(@part)/2 -1, ''))结尾去

Anybody have a nice tight and efficient SQL Server function that will return the fist string (terminated by a whitespace) following the first match of a given string.

I've got some code, but it's real ugly and probably slow.

For example, in In test 12545 file:x12545.jpg appears to be good given file: would return x12345.jpg

Thanks.

解决方案

create function dbo.extractAfter(@full nvarchar(max), @part nvarchar(max))
returns nvarchar(max)
with returns null on null input
as begin
return ltrim(stuff(left(@full,charindex(' ', @full + ' ', charindex(@part,@full)+1)),
    1, charindex(@part,@full)+datalength(@part)/2 -1, ''))
end
go

这篇关于SQL Server 2005 函数提取一组字符串后的子字符串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆