Skip to content

[FEATURE] Allow trimming of other characters than spaces. #701

Description

@mordio

Is your feature request related to a problem? Please describe.
Since SQL Server 2022 LTRIM and RTRIM support a second argument characters to remove other characters than just spaces. TRIM needs a different syntax.

Describe the solution you'd like
At least for LTRIM AND RTRIM it should be fairly easy to support the additional argument and therefore allow trimming of arbitrary characters.

Describe alternatives you've considered
Alternatives are possible with a set of 4 REPLACEs or just doing it client side.

Additional context
I think this should work:

/// <summary>
/// Removes a set of characters from the start of a string
/// </summary>
/// <param name="expression">A character expression where characters should be removed</param>
/// <param name="characters">A set of characters to be removed from the stadt and end of <paramref name="expression"/></param>
/// <returns></returns>
[SqlFunction(IsDeterministic = true)]
public static SqlString LTrim([MaxLength] SqlString expression, SqlString characters)
{
    if (expression.IsNull || characters.IsNull)
        return SqlString.Null;

    return new SqlString(expression.Value.TrimStart(characters.Value.ToCharArray()), expression.LCID, expression.SqlCompareOptions);
}

/// <summary>
/// Removes a set of characters from the end of a string
/// </summary>
/// <param name="expression">A character expression where characters should be removed</param>
/// <param name="characters">A set of characters to be removed from the stadt and end of <paramref name="expression"/></param>
/// <returns></returns>
[SqlFunction(IsDeterministic = true)]
public static SqlString RTrim([MaxLength] SqlString expression, SqlString characters)
{
    if (expression.IsNull || characters.IsNull)
        return SqlString.Null;

    return new SqlString(expression.Value.TrimEnd(characters.Value.ToCharArray()), expression.LCID, expression.SqlCompareOptions);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions