How to replace embedded commas in a string in a table

Coreysan 1,811 Reputation points
2023-06-30T21:40:54.0366667+00:00

I imported 20K lines of data from a CSV file into a table (wline varchar(4000)). For any field that has surrounding double quotes, is there a way to write in T-SQL a routine to find embedded commas between dbl-quotes, and change them to something else (like a caret), then I can remove all the dbl-quotes?

E.g., 123,345,serg,65,rthstrh,"erwfre, sertert", 345q345,ergerg, "123 anwhere, CA", waefwaef, ertwert

                  change to:                 ^                                                              ^
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-06-30T22:04:45.21+00:00

    How did you import the file? If you import the file with BULK INSERT, you can specify FORMAT=CSV, and BULK INSERT will nice divide the files into columns.

    The operation you suggest is definitely nothing I would like to do in in T-SQL. It is not well-equipped for advanced string manipulation.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.