wtorek, 10 listopada 2015

Removing aliases from a SQL select statement



private string RemoveAliases(string sqlSelectString)
{
    sqlSelectString = sqlSelectString.ToLower();
    
    const string ALIAS_INDICATOR = " as ";
    int aliasIndex;
    int commaIndex;
    string modifiedSqlString;

    do
    {
        modifiedSqlString = sqlSelectString;
        aliasIndex = sqlSelectString.IndexOf(ALIAS_INDICATORSystem.StringComparison.Ordinal);
        if (aliasIndex != -1)
        {
            string columnTextExpressionAlias = sqlSelectString.Substring(aliasIndex);

            commaIndex = columnTextExpressionAlias.IndexOf(","System.StringComparison.Ordinal);

            if (commaIndex != -1)
            {
                sqlSelectString = sqlSelectString.Remove(aliasIndexcommaIndex);
            }
            else
            {
                sqlSelectString = sqlSelectString.Remove(aliasIndex, columnTextExpressionAlias.Length);
            }
        }
    }
    while (sqlSelectString != modifiedSqlString);

    return sqlSelectString;
}

Brak komentarzy:

Prześlij komentarz