SQL SERVER: substring-before substring-after

Just a quick reference to put in my delicio.us tag collection.

———————————
CREATE FUNCTION [dbo].[fsg_substring-before](
@what_to_search nvarchar(256),
@where_to_search nvarchar(256))

— NB: substring before FIRST occurrence

RETURNS nvarchar(256)
AS
BEGIN
return LEFT(@where_to_search, CHARINDEX(@what_to_search,@where_to_search)-1)

END
————————–
CREATE FUNCTION [dbo].[fsg_substring-after](
@what_to_search nvarchar(256),
@where_to_search nvarchar(256))

— NB: substring after FIRST occurrence

RETURNS nvarchar(256)
AS
BEGIN

return right(@where_to_search,len(@where_to_search)-charindex(@what_to_search,@where_to_search))

END

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s