Well, this is really awful, but okay, if you are going to refuse to even consider better alternatives... first, create a set-based split function that will track the order of the string (your looping function is really not optimal):
CREATE FUNCTION [dbo].[SplitStrings_Ordered]
(
@List VARCHAR(8000),
@Delimiter VARCHAR(255)
)
RETURNS TABLE
AS
RETURN (SELECT [Index] = ROW_NUMBER() OVER (ORDER BY Number), Item
FROM (SELECT Number, Item = SUBSTRING(@List, Number,
CHARINDEX(@Delimiter, @List + @Delimiter, Number) - Number)
FROM (SELECT ROW_NUMBER() OVER (ORDER BY [object_id])
FROM sys.all_columns) AS n(Number)
WHERE Number <= CONVERT(INT, LEN(@List))
AND SUBSTRING(@Delimiter + @List, Number, LEN(@Delimiter)) = @Delimiter
) AS y);
(If you have a numbers table in this database, use that instead of sys.all_columns
, and add WITH SCHEMABINDING
to the function definition.)
Now, let's look at a few examples of strings with commas embedded inside double quotes, and removing those before splitting and re-concatenating:
DECLARE @x TABLE(n VARCHAR(8000));
INSERT @x VALUES
('0150566115,"HEALTH 401K","IC,ON","ICON HEALTH 401K",,,1,08/21/2014'),
('0150566115,HEALTH 401K,"IC,ON","ICON HEALTH 401K",,,1,"08/21/2014"'),
('"01505,66115,","HEALTH 401K","IC,ON","ICON HEALTH 401K",,,1,08/21/2014');
;WITH x AS
(
SELECT x.n, s.[Index], s = REPLACE(s.Item, ',',
CASE s.[Index]%2 WHEN 0 THEN '' ELSE ',' END)
FROM @x AS x
CROSS APPLY dbo.SplitStrings_Ordered(x.n, '"') AS s
)
SELECT x.n, fixed = (SELECT x2.s
FROM x AS x2
WHERE x2.n = x.n
ORDER BY [Index]
FOR XML PATH, TYPE).value(N'.[1]',N'varchar(max)')
FROM x
GROUP BY x.n;
Results in the fixed
column for all three strings:
0150566115,HEALTH 401K,ICON,ICON HEALTH 401K,,,1,08/21/2014
0150566115,HEALTH 401K,ICON,ICON HEALTH 401K,,,1,08/21/2014
0150566115,HEALTH 401K,ICON,ICON HEALTH 401K,,,1,08/21/2014
Now, you can feed those results back into the split function, using the comma this time, depending on your ultimate goal. The question seemed to resolve only around being able to ignore the double quotes and any commas contained only inside double-quote pairs.
For more on splitting and concatenating strings:
For more on numbers tables and generating sets without loops:
Edit: tt is a valid format specifier for datetime, but not time. The \ is also optional in datetime, but not time. Example at end.
I think the tt is the problem
I am not finding tt for time formatting - what is it supposed to do?
Did you try without using @FormatString?
Below works
declare @FormatString nvarchar(200) = N'hh\:mm'
declare @TimeChar nvarchar(200) = N'10:27'
select @FormatString
select @TimeChar
select convert(time, @TimeChar)
select format(convert(time, @TimeChar), N'hh\:mm')
select format(convert(time, @TimeChar), @FormatString)
Example:
select format(convert(time , '10:27'), N'hh\:mm') -- returns 10:27
, format(convert(time , '10:27'), N'hh:mm') -- returns NULL (need to escape ':'):
, format(convert(time , '10:27'), N'hh:mm tt') -- returns NULL
, format(convert(datetime, '10:27'), N'hh:mm tt') -- returns 10:27
Best Answer
You could try something like this
DB<>Fiddle