2009-10-22

Single Quotes in TSQL

Recently I had a to format some data, originally the formatting was done using the MVC.NET page but there was one for small problem it took over five minutes to return 2,000 records which is just a little bit to long for my liking.

We only had a day to fix this problem so it was time for a dirty yet brilliant hack. I created a table variable and for the formatted data added the data to it and returned the date all in one stored procedure, there was one problem tho I needed to generate T-SQL and run it using EXECUTE (I know its not the best way to do things). I was working away at this and then a problem popped up there is no way to escape single quotes that I could work out in T-SQL.

But with a little trickery.....

DECLARE @singleQuote CHAR(1);
SET @singleQuote = CHAR(39);

Problem Solved!