I am in the process of rolling over a bunch of old stored procedures that take NVARCHAR(MAX)
strings of comma and/or semicolon separated values (never mind about one value per variable etc.). The code is currently using the CHARINDEX
approach described in this question, though in principle any of the approaches would work (I'm tempted to replace it with the XML one, because neatness).
The question, though, is what is the most efficient may of handing escaped delimiters? Obviously the lowest level approach is a character by character parser, but I can't shake the feeling that (1) that's going to be horrible when executed a million times in close succession and (2) it'll be overcomplicated for the situation.
Basically, I want to handle 3 possible escapes:
"\\", "\,", and "\;" somewhere in my string. What's the best way to do it? I should add that, ideally, I don't want to make any assumptions about what characters are included in the string.
Sample data would look something like the below.
Value1,Value\,2,ValueWithSlashAtTheEnd\\,ValueWithSlashAndCommaAtTheEnd\\\,
I'm actually splitting to rows rather than columns, but the principle is the same; I'd expect the below output typically:
SomeName
^^^^^^^^
Value1
Value,2
ValueWithSlashAtTheEnd\
ValueWithSlashAndCommaAtTheEnd\,
Needless to say, the escapes could occur anywhere in a value, and ideally I'd like to handle for semicolons as well, but I'll probably be able to infer that from the comma behaviour.