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.

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Seems to work, but need some help tweaking it. Can we return null if no match? Also, can we include < as delimiter (due to possible hidden html tags) so that file:x12567.jpg<br> would not pick up the tag. Thanks!
datalength(@part)/2 could also be length(@part + '.') - 1. The former may be faster, while the latter is more universal (can work both with nvarchar and varchar).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.