SQL Server 2005 function to extract a substring ma

2019-08-07 04: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