SQL Server 2005 function to extract a substring match following a set string

安稳与你 提交于 2020-02-05 08:13:09

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/5431191/sql-server-2005-function-to-extract-a-substring-match-following-a-set-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!