///
/// I wrote this because we had a legacy database that stored multiple
/// address fields in a single column (as in 'High Street|London|E1 1AA',
/// but somewhat longer) and we needed to retrieve a single field in a
/// stored procedure.
///
/// The name of the database column.
/// The field separator character.
/// The number of fields in the column.
/// Which field we want to retrieve.
/// Some really nasty SQL.
private string GetFieldSplitterSql(string colName, char sep, int numFields, int fieldToGet)
{
string s = colName;
for (int i = 0; i < fieldToGet; i++)
{
s = "SUBSTRING(" + s + ", 1 + CHARINDEX('" + sep + "', " + s + "), 999)";
}
if (fieldToGet < numFields) // not the last field
{
s = "SUBSTRING(" + s + ", 1, CHARINDEX('" + sep + "', " + s + ") - 1)";
}
return s;
}