Skip to content

Commit bce2021

Browse files
author
jeffshumphreys@gmail.com
committed
Added a SplitTo8 function since I had a use case for it.
1 parent a9a28a0 commit bce2021

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

StringSplit.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,66 @@ public Pieces4Record(string lcol1, string lcol2, string lcol3, string lcol4)
106106
}
107107
}
108108

109+
[SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, IsPrecise = true, FillRowMethodName = nameof(Pieces8AsSQLRow), TableDefinition = "col1 NVARCHAR(MAX), col2 NVARCHAR(MAX), col3 NVARCHAR(MAX), col4 NVARCHAR(MAX), col5 NVARCHAR(MAX), col6 NVARCHAR(MAX), col7 NVARCHAR(MAX), col8 NVARCHAR(MAX)")]
110+
public static IEnumerable SplitTo8ColumnsX(string input, string pattern)
111+
{
112+
MatchCollection regexmatches = Regex.Matches(input, pattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(10));
113+
int nofmatches = regexmatches.Count;
114+
string[] distinctfieldvalues = new string[8];
115+
116+
var rowoffields = new List<Pieces8Record>(1);
117+
if (nofmatches < 1) return rowoffields.ToArray();
118+
int j = 0;
119+
foreach (Group group in regexmatches[0].Groups)
120+
{
121+
if (j == 0) { j++; continue; } // Skip the first global capture
122+
var fieldvalue = group.ToString();
123+
distinctfieldvalues[j - 1] = fieldvalue; // Store in 0th element
124+
j++;
125+
}
126+
127+
Pieces8Record fieldsasrecord = new Pieces8Record(distinctfieldvalues[0], distinctfieldvalues[1], distinctfieldvalues[2], distinctfieldvalues[3]
128+
, distinctfieldvalues[4], distinctfieldvalues[5], distinctfieldvalues[6], distinctfieldvalues[7]);
129+
rowoffields.Add(fieldsasrecord);
130+
return rowoffields.ToArray();
131+
}
132+
133+
// Called from SQL Server only
134+
private static void Pieces8AsSQLRow(Object obj, out SqlString col1, out SqlString col2, out SqlString col3, out SqlString col4, out SqlString col5, out SqlString col6, out SqlString col7, out SqlString col8)
135+
{
136+
var pieces = obj as Pieces8Record;
137+
col1 = pieces.col1;
138+
col2 = pieces.col2;
139+
col3 = pieces.col3;
140+
col4 = pieces.col4;
141+
col5 = pieces.col5;
142+
col6 = pieces.col6;
143+
col7 = pieces.col7;
144+
col8 = pieces.col8;
145+
}
146+
147+
public class Pieces8Record
148+
{
149+
public string col1;
150+
public string col2;
151+
public string col3;
152+
public string col4;
153+
public string col5;
154+
public string col6;
155+
public string col7;
156+
public string col8;
157+
158+
public Pieces8Record(string lcol1, string lcol2, string lcol3, string lcol4, string lcol5, string lcol6, string lcol7, string lcol8)
159+
{
160+
col1 = lcol1;
161+
col2 = lcol2;
162+
col3 = lcol3;
163+
col4 = lcol4;
164+
col5 = lcol5;
165+
col6 = lcol6;
166+
col7 = lcol7;
167+
col8 = lcol8;
168+
}
169+
}
109170
}
110171
}

0 commit comments

Comments
 (0)