0

I have a string that looks like this:

/Company/Business 1/Business 2/Business 3/Business 4...

And I am looking to create a substring where I only extract the information for Business 2

Can anyone help? I am having issues because the information in Business 3 and Business 4 are varying lengths

Palisand
  • 1,302
  • 1
  • 14
  • 34

1 Answers1

0

Below solution using SQL Query:

I have created SQL function to Split and search by record place,

Function params are Input String, Delimiter char and Record Number

SQL Function -

CREATE FUNCTION [dbo].[SplitString]  ( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1),     @record int  )  RETURNS @output TABLE(splitdata NVARCHAR(MAX)  )  BEGIN 
    DECLARE @start INT, @end INT, @rownumber INT = 0 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1     BEGIN       SET @rownumber = @rownumber + 1
        IF @end = 0  
            SET @end = LEN(@string) + 1

        IF(@rownumber = @record)        BEGIN           INSERT INTO @output (splitdata)             VALUES(SUBSTRING(@string, @start, @end - @start))       END
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)

    END 
    RETURN  END

Select Query -

SELECT * FROM dbo.SplitString('/Company/Business 1/Business 2/Business 3/Business 4','/',4)

Result :

Business 2

Dhana
  • 1,618
  • 4
  • 23
  • 39
  • Note : This assumes MS SQL Server. If it is SQL Server, I'd read this answer for splitting strings; https://stackoverflow.com/questions/29961576/how-can-i-convert-split-function-to-inline-table-valued-udf-in-sql-server Using this with `ItemNumber = 4` is going to be just about as fast as you can get. – MatBailie Oct 04 '18 at 22:15