Below user-defined scalar function help you to wrap a string in single quotes for a dynamic TSQL statement.
1 2 3 4 5 6 7 8 9 10 11 12 |
USE [databasename]; GO CREATE FUNCTION [dbo].[Quoted_String](@string nvarchar(3500)) RETURNS nvarchar(4000) AS BEGIN DECLARE @tmp_string nvarchar(4000) DECLARE @quote char(1) SELECT @quote = '''' SELECT @tmp_string = replace(@string, @quote, @quote + @quote) RETURN(@quote + @tmp_string + @quote) END |
Leave a Comment