Search code examples
sql-servervarchar

Convert VARCHAR of "a,b,c" to "'a','b','c'"


i have set of values like aaa,bbb,ccc,ddd,eee to 'aaa','bbb','ccc','ddd','eee'. what is the best way to do this?


Solution

  • If VARCHAR value all have the same format as your example

    You could use the following REPLACE()

    declare @str VARCHAR(1000) = 'aaa,bbb,ccc,ddd,eee'
    SELECT '''' + REPLACE(@str,',',''',''') + ''''