Add quotes around variable

2020-04-19 06:10发布

问题:

I need to export data from SQL and import into SAS. The address field has ',' in the middle of the string. I tried using CSV and tab delimited, but each time SAS breaks up the address field due to the ','.

I tried replacing the comma with a space using code from another question, but it did not work:

 update #temp2
 set STREETADDRESS_e = REPLACE(STREETADDRESS_e ,","," ")

I thought if I put the address string in quotes, this would solve the problem, but my code is not working:

 update #temp2
 set STREETADDRESS_e = ("'" + STREETADDRESS_e + "'")

This seems like it must be a very common problem but I have not found any working solutions...

回答1:

If you want to surround the string with single-quotes you have to escape them like this:

update #temp2 set STREETADDRESS_e = ('''' + STREETADDRESS_e + '''')

or

update #temp2 set STREETADDRESS_e = QUOTENAME(STREETADDRESS_e,'''')

or if you want double-quotes

update #temp2 set STREETADDRESS_e = QUOTENAME(STREETADDRESS_e,'"')