Skip to content
Advertisement

Extract string between double quotes in SQL

I have a string and I need to return the characters between the double quotes

‘((“Name 1” and “Name 2”) or “Name 3”)

I need to return

Name 1
Name 2
Name 3

I have used the function below to split the string but I am getting the (( and the Or and AND etc .. which I don’t want and unfortunately I can’t be certain of all the other characters that could be included so removing them or replacing them isn’t really feasible.

ALTER FUNCTION [dbo].[fn_SplitString] 
( 
   @string NVARCHAR(MAX), 
   @delimiter CHAR(1) 
) 
RETURNS @output TABLE(ID int, splitdata NVARCHAR(MAX) 
) 
BEGIN 
   DECLARE @start INT, @end INT, @Count INT
   set @Count = 1
   SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
   WHILE @start < LEN(@string) + 1 BEGIN 
      IF @end = 0  
         SET @end = LEN(@string) + 1

       INSERT INTO @output (ID, splitdata)  
       VALUES(@Count, SUBSTRING(@string, @start, @end - @start)) 
       SET @start = @end + 1 
       SET @end = CHARINDEX(@delimiter, @string, @start)
       Set @Count = @Count+1
   END 
   RETURN 
END

I know this code will return the string between 2 delimiters

substring( LEFT(@String, charindex(']', @String)-1), CHARINDEX('[', @String) + len('['), LEN(@String)) 

is there any way to combine the 2 and return the required output?

Thanks

Advertisement

Answer

Give this a try; I kept the debugging variables in the output table

ALTER FUNCTION [dbo].[fn_SplitString] 
( 
    @string     NVARCHAR(MAX), 
    @delimiter  CHAR(1) 
) 
RETURNS @output TABLE
(
    [Id]        INT,
    [Start]     INT,
    [End]       INT,
    [Length]    INT,
    [Data]      NVARCHAR(MAX)
) 
BEGIN 
    DECLARE @count INT, @start INT, @end INT
    SELECT  @count = 1, @end = 0,
            @start = CHARINDEX(@delimiter, @string)
    WHILE   @start > 0 BEGIN 

        SELECT  @end = CHARINDEX(@delimiter, @string, @start + 1)

        INSERT INTO @output ([Id], [Start], [End], [Length], [Data])
        VALUES (@count, @start, @end, @end - @start - 1, 
                SUBSTRING(@string, @start + 1, @end - @start - 1)) 

        SELECT  @start = CHARINDEX(@delimiter, @string, @end + 1), 
                @count = @count + 1
    END 
    RETURN 
END

Or this, based on the idea of receiving two different delimiters:

ALTER FUNCTION [dbo].[fn_SplitString] 
( 
    @string     NVARCHAR(MAX), 
    @delimiter1 NVARCHAR(MAX),
    @delimiter2 NVARCHAR(MAX)
) 
RETURNS @output TABLE
(
    [Id]        INT,
    [Start]     INT,
    [End]       INT,
    [Length]    INT,
    [Data]      NVARCHAR(MAX)
) 
BEGIN 
    DECLARE @count INT, @start INT, @end INT
    SELECT  @count = 1, @end = 0
    SELECT  @start = CHARINDEX(@delimiter1, @string)
    SELECT  @end   = CHARINDEX(@delimiter2, @string, @start + 1)

    WHILE   @start > 0 AND @end > 0 BEGIN 

        INSERT INTO @output ([Id], [Start], [End], [Length], [Data])
        VALUES (@count, @start, @end, @end - @start - 1, 
                SUBSTRING(@string, @start + 1, @end - @start - 1)) 

        SELECT  @start = CHARINDEX(@delimiter1, @string, @end + 1)
        SELECT  @end   = CHARINDEX(@delimiter2, @string, @start + 1),
                @count = @count + 1
    END 
    RETURN 
END
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement