I need help with the format for single qoutes. I am using dynamic sql in an SSRS for error trapping records inserted with single qoutes during bulk imports but I cannot seem to format the actual select statement properly.

SQL Sever 2005 Dev edition. Any help is great.

Declare @DB varchar(99) ------------not used in actual report only for testing in ssms
Set @DB = 'TestDb1' ------------not used in actual report only for testing in ssms
declare @sqlstring nvarchar (3000)
set @sqlstring = 'use ' + @DB + '(select * from Address where Customer like ''%''%'')'
execute sp_executesql @sqlstring

Recommended Answers

All 3 Replies

Change your fourth line to look like this:

set @sqlstring = 'use ' + @DB + '(select * from Address where Customer like ''%''''%'')'

Notice that there are now four single-quotes between the percent-signs. I tested this in SQL2005 Standard Edition, but it should also work with Developer Edition.

Thank you man! Definitely life saver............I honestly wrapped my head around that for about 5 hours

Glad to help. Be sure to mark the thread "Solved".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.