double qoutes issue

Shambhu Rai 1,411 Reputation points
2024-01-24T13:08:48.6533333+00:00

Hi Expert,

I want to remove the double qoutes from end and start of the statement

create table table1 ( col1 varchar(200))

create table  table1 ( col1 varchar(200))
insert into tale1 values('"Test1 Test1 Test1 "Test1" Test1"')
insert into tale1 values('"Test1 "Test1" Test1 Test1 Test1"')
insert into tale1 values('"Test1 Test1 Test1 Test1 "Test1"')
expected output:
Test1 "Test1" Test1 Test1 Test1
Test1 Test1 Test1 "Test1" Test1
Test1 Test1 Test1 Test1 "Test1" 
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,492 questions
SQL Server Reporting Services
SQL Server Reporting Services
A SQL Server technology that supports the creation, management, and delivery of both traditional, paper-oriented reports and interactive, web-based reports.
3,016 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,693 questions
{count} votes

5 answers

Sort by: Most helpful
  1. Viorel 120.7K Reputation points
    2024-01-24T13:31:43.84+00:00

    Check if this query is suitable in your case:

    select trim('"' from col1) as result from table1
    

  2. Shambhu Rai 1,411 Reputation points
    2024-01-24T23:31:19.5466667+00:00

    This would be trim output Test1 Test1 Test1 Test1 "Test1 in case we have double qoutes on last word then it did not work


  3. LiHongMSFT-4306 31,311 Reputation points
    2024-01-25T06:33:30.08+00:00

    Hi @Shambhu Rai

    Have read all your comments, but still confused about your request.

    For this row: "Test1 Test1 Test1 "Test1" Test1"

    And you want this output: Test1 "Test1" Test1 Test1 Test1

    You have post this:

    1. remove the double qoutes from end and start of the statement.
    2. the original data contains one word in double quotes but not fix wheather it is in last or in between. All three rows are different

    You still need to explain why the second Test1 was double quoted in the output.

    Best regards,

    Cosmog Hong


  4. Yitzhak Khabinsky 26,471 Reputation points
    2024-01-25T21:46:35.0866667+00:00

    Hi @Shambhu Rai, It seems that I finally got it what you need. Please try the following solution based on XML and XQuery. Overall, we are tokenizing string of tokens and via if/else logic making decision what to do with each token.

    -- DDL and sample data population, start
    DECLARE @tbl TABLE  (ID INT IDENTITY PRIMARY KEY, tokens VARCHAR(200));
    INSERT @tbl (tokens) VALUES
    ('"Test1 Test1 Test1 "Test1" Test1"'),
    ('"Test1 "Test1" Test1 Test1 Test1"'),
    ('"Test1 Test1 Test1 Test1 "Test1"');
    -- DDL and sample data population, end
    
    DECLARE @separator CHAR(1) = SPACE(1);
    
    SELECT * FROM @tbl;
    
    SELECT t.* , c
    	, c.query('
    		for $x in /root/r/text()
    		return if (substring($x,1,1)="""" and substring($x,string-length($x),1)="""") then string($x)
    			else if (substring($x,1,1)="""" and substring($x,string-length($x),1)!="""") then string(substring($x,2, 200))
    			else if (substring($x,1,1)!="""" and substring($x,string-length($x),1)="""") then string(substring($x,1, string-length($x)-1))
    			else string($x)
    	
    	').value('.','VARCHAR(200)') AS result
    FROM @tbl AS t
    CROSS APPLY (SELECT TRY_CAST('<root><r><![CDATA[' + 
        REPLACE(tokens, @separator, ']]></r><r><![CDATA[') + 
        ']]></r></root>' AS XML)) AS t1(c)
    
    

  5. Adam Bohil 61 Reputation points
    2024-02-06T19:59:47.4966667+00:00

    ...

    SELECT TRIM('"' FROM col1)
    FROM table1
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.