I have a string like this:
'one, two, three'
How can I get it to look like this:
'one','two','three'
so I can use it in an IN clause?
I have a string like this:
'one, two, three'
How can I get it to look like this:
'one','two','three'
so I can use it in an IN clause?
Simply use the REPLACE method. Replace comma with the required string
DECLARE @TestData AS VARCHAR (200) = '''one, two, three''';
DECLARE @ReplacedData AS VARCHAR (200) = '';
SELECT @ReplacedData = REPLACE(@TestData, ', ', ''',''')
If you want to use the @TestData
in the IN
, you need to use the dynamic query like below:
DECLARE @SqlTest AS VARCHAR (MAX) = '';
SET @SqlTest = 'SELECT * FROM TestTable WHERE ColumnValue IN (' + @ReplacedData + ')'
EXEC (@SqlTest)