Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9292681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:56:20+00:00 2026-06-18T20:56:20+00:00

Context: SQL Server 2000 I’ve written a UDF that gives me the text between

  • 0

Context: SQL Server 2000

I’ve written a UDF that gives me the text between two other texts, viz

CREATE FUNCTION dbo.StrBetween 
(
    @Text nvarchar(4000),
    @Lhs nvarchar(4000),
    @Rhs nvarchar(4000)
)
RETURNS nvarchar(4000)
AS
BEGIN
    DECLARE @LhsOffset INT;
    DECLARE @RhsOffset INT;
    DECLARE @Result NVARCHAR(4000);

    SET @LhsOffset = CHARINDEX( @Lhs, @Text );
    IF @LhsOffset = 0 
    BEGIN
        RETURN @Text;
    END
    SET @Result = SUBSTRING( @Text, @LhsOffset+1, LEN(@Text)-LEN(@Lhs));
    SET @RhsOffset = CHARINDEX( @Rhs, @Result );
    IF @RhsOffset = 0 
    BEGIN
        RETURN @Result;
    END
    SET @Result = SUBSTRING( @Result, 1, @RhsOffset - 1 );
    RETURN @Result;
END

This works fine in SQL Query Analyser if I have, say,

SELECT dbo.StrBetween('dog','d','g')

However, when I pass a column in as the value of the first argument, I get no response. For example,

SELECT [TEST].[dbo].StrBetween(Referrer,'//', '/') as tst FROM tblTest 

Referrer is declared as an nvarchar field. I’m a newbie when it comes to T-SQL. What obvious thing am I not seeing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:56:22+00:00Added an answer on June 18, 2026 at 8:56 pm

    It’s not an issue with calling – it’s a logic issue, and the fact that your @Rhs value is part of the @Lhs value.

    SET @Result = SUBSTRING( @Text, @LhsOffset+1, LEN(@Text)-LEN(@Lhs));
    

    This is removing the first character of your @Lhs string. However, since the second character is /, and that’s what your @Rhs match is searching for, it immediately finds it at position 1 and so you get an empty string.

    Instead, try:

    SET @Result = SUBSTRING( @Text, @LhsOffset+LEN(@Lhs), 4000);
    

    You don’t have to be exact with computing a length. If you ask for 4000 characters and the string is only 12 characters long, SUBSTRING will give you back at most 12 characters. So don’t bother computing the new length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got data in SQL Server 2000 and have a HyperLink that goes to
Context: ASP.NET MVC 2.0, C#, SQL Server 2008, IIS7 I have 'scheduledMeetings' table in
I have a LINQ TO SQL Context that I have created that calls a
This is a design question, I noticed that by time by LINQ-to-SQL Context gets
Code for SQL class as follows: import java.text.SimpleDateFormat; import java.util.Date; import android.content.ContentValues; import android.content.Context;
I have developed an application using Entity Framework , SQL Server 2000, Visual Studio
I am working in ASP.Net MVC 1.0 and SQL Server 2000 using Entity Framework.
I'm accessing a data context object that is auto-generated by using LINQ to SQL.
I have a table in a SQL server 2000 database with a nvarchar(30) field
I am using SQL server MSDE 2000. I have a field called notes of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.