sqlsql-serversql-server-2012sql-server-2008-r2sql-server-2014-express

Single quotes for strings in SQL Server


DECLARE @rpmProvider varchar(200) = 'Prudhvi, raj,Lalith, Kumar';

DECLARE @xml xml
DECLARE @XMT_TEXT AS VARCHAR(200)

SET @XMT_TEXT = REPLACE(REPLACE(REPLACE(''''+ @rpmProvider + '''', ', ', '|'), ',', '</X><X>'), '|', ', ')
SET @xml = cast(('<X>' + @XMT_TEXT + '</X>') as xml)


SELECT N.value('.', 'varchar(50)') as value
FROM @xml.nodes('X') as T(N);

My output :

1. 'Prudhvi, raj
2. Lalith, Kumar'

Expected output :

1. 'Prudhvi, raj'
2. 'Lalith, Kumar'

Solution

  • try like below

     DECLARE @rpmProvider varchar(200) = 'Prudhvi, raj,Lalith, Kumar';
    
    DECLARE @xml xml
    DECLARE @XMT_TEXT AS VARCHAR(200)
    
    SET @XMT_TEXT = REPLACE(REPLACE(REPLACE(''''+ @rpmProvider + '''', ', ', '|'), ',', '</X><X>'), '|', ', ')
    SET @xml = cast(('<X>' + @XMT_TEXT + '</X>') as xml)
    
    
    SELECT case when left( N.value('.', 'varchar(50)'),1)=''''  then '' else
    '''' end + N.value('.', 'varchar(50)')  +
    case when right( N.value('.', 'varchar(50)'),1)=''''  then '' else
    '''' end as value
    FROM @xml.nodes('X') as T(N);
    
    
    
     value
    'Prudhvi, raj'
    'Lalith, Kumar'
    

    demo link