t-sql replacing double quotes

11,628

look at this code:

DECLARE @X  varchar(20)
SET @X='''''Hello"'

PRINT @X
PRINT REPLACE(@X,'''''','"')
PRINT REPLACE(REPLACE(@X,'''''',''''),'"','''')

here is the output:

''Hello"
"Hello"
'Hello'

SQL Server does not escape quotes with slashes, a single quote is escaped with another single quote. This will print one single qoute:

print ''''

this will print two single quotes:

print ''''''
Share:
11,628
Blerta
Author by

Blerta

I have been working for several years as a Developer now. It is definitely a challenging and interesting experience. I am intersted in : C# and Asp.net Developer Ajax, Xml Microsoft Sql Server, MySql Php, Javascript, Html, CSS

Updated on June 04, 2022

Comments

  • Blerta
    Blerta almost 2 years

    I have a t-sql statement like Replace(field,'\''','\"') because i have two different results ''field1'' and "field2" but what if i consider those two different results the same and want to group them. I choose to group those two by replacing the first double quotes with the second style but although replaced they are not interpreted as the same type of quote.

    What I am missing here??

    Edited: I am trying to group data where text is the same but quotes differ, user is entering two single quotes ''hello'' and one double quote "hello", if I have this two rows I am trying to display them as one as "hello", so by executing the above statement I think I should be able to do this, but it isn't working properly even without slashes.