Question

I am trying to write my own CLR function to replace the built in 'TRY_CONVERT' sql function as I need more control over how dates and numbers are converted (e.g. the built-in function can't handle DECIMAL conversions that contain scientific notation).

I have tried this:

[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static object TRY_CONVERT(SqlDbType type, SqlString input)
{
    switch (type)
    {
        case SqlDbType.Decimal:
            decimal decimalOutput;
            return decimal.TryParse(input.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out decimalOutput) ? decimalOutput : (decimal?)null;
        case SqlDbType.BigInt:
            long bigIntOutput;
            return long.TryParse(input.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out bigIntOutput) ? bigIntOutput : (long?)null;
        case SqlDbType.Date:
        case SqlDbType.DateTime:
        case SqlDbType.DateTime2:
            DateTime dateTimeOutput;
            return DateTime.TryParse(input.Value, CultureInfo.CreateSpecificCulture("en-GB"), DateTimeStyles.None, out dateTimeOutput) ? dateTimeOutput : (DateTime?)null;
        case SqlDbType.NVarChar:
        case SqlDbType.VarChar:
            return string.IsNullOrWhiteSpace(input.Value) ? null : input.Value;
        default:
            throw new NotImplementedException();
    }
}

but it doesn't like the SqlDbType type when I build.

Is it possible to pass a 'target_type' as used in the built-in function or will I have to pass it as a string or create separate TRY_CONVERT methods for each type I want to use?

Was it helpful?

Solution

The object return type translates to sql_variant so would have to be converted explicitly to the correct data type in SQL so I think the only way to solve this is to create separate CLR Methods that have the correct return types like this:

[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlDecimal TRY_CONVERT_DECIMAL(SqlString input)
{
    decimal decimalOutput;
    return !input.IsNull && decimal.TryParse(input.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out decimalOutput) ? decimalOutput : SqlDecimal.Null;
}

[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlInt64 TRY_CONVERT_BIGINT(SqlString input)
{
    long bigIntOutput;
    return !input.IsNull && long.TryParse(input.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out bigIntOutput) ? bigIntOutput : SqlInt64.Null;
}

[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlDateTime TRY_CONVERT_DATE(SqlString input)
{
    var minSqlDateTime = new DateTime(1753, 1, 1, 0, 0, 0, 0);
    var maxSqlDateTime = new DateTime(9999, 12, 31, 23, 59, 59, 0);
    DateTime dateTimeOutput;
    return !input.IsNull && DateTime.TryParse(input.Value, CultureInfo.CreateSpecificCulture("en-GB"), DateTimeStyles.None, out dateTimeOutput) &&
        dateTimeOutput >= minSqlDateTime && dateTimeOutput <= maxSqlDateTime ? dateTimeOutput : SqlDateTime.Null;
}

[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlString TRY_CONVERT_NVARCHAR(SqlString input)
{
    return input.IsNull || string.IsNullOrWhiteSpace(input.Value) ? SqlString.Null : input.Value;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top