开发者

Comma Delimiting Names with Apostrophes

This is a T-SQL question.

I have a select statement where i take a list of cities and comma delimit them, then put them into a temp table. However, one of my cities is Couer D'Alene, which has an apostrophe in its name, and SQL is not liking that - no results are returned. How can I modify this to accept city names with apostrophes? Thanks!

Declare @vCity varchar(1000)

Select @vCity= ',' + @vCity+ ','

create table #Cities
(
    cityName varchar (1000)
)

Insert Into #Cities
Select  cityName
From cityTab开发者_如何转开发le
Where  @vCity Like  '%,' + cityName + ',%'
Group By cityName


You need to escape the ' with another ':''

Easiest way is the REPLACE function:

Select @vCity= ',' + REPLACE(@vCity,'''', '''''' ) + ','

All the extra ' are escaping several layers deep.


select QUOTENAME( cityName, '''' ) ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜