2

I have a long string containing three special symbols ' @ and ? and some text are enclosed inside these symbols.

for ex. "@sa@32@ddd@?222?@ds@asa@hhh@?ddsds?dsdsd?cccc?'cxcx'?ccxc?cxc?'cxcx'"

I want the values sa 32 ddd 222 ds etc.. and insert these values in a table. Next step is to insert these values inside different column of same row in a table.

How can I achieve that.

Sanjeev Singh
  • 3,976
  • 3
  • 33
  • 38

2 Answers2

2

The following function allows you to split the given string on the basis of a single delimiter.

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

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

  END 
  RETURN 
END

You can use this function iteratively for each of the individual delimiters, over each individual string obtained from the first run and the final output should provide you the requisite values.

Shubham Pandey
  • 919
  • 1
  • 9
  • 19
1

Try this way. The complete answer It depends od more information

SET QUOTED_IDENTIFIER OFF

declare @test varchar(100)

set @test ="@sa@32@ddd@?222?@ds@asa@hhh@?ddsds?dsdsd?cccc?'cxcx'?ccxc?cxc?'cxcx'"

print replace(replace(@test,'@',' '),'?',' ')
Krismorte
  • 642
  • 7
  • 24