我试图写我自己的CLR函数来替换内置的“TRY_CONVERT” SQL函数,因为我需要在如何日期和数字转换更多的控制(例如,内置函数不能处理包含科学记数法十进制转换)。
我曾经尝试这样做:
[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();
}
}
但它不喜欢的SqlDbType
当我建立的类型。
是否有可能通过在内置功能中使用或我必须把它作为一个字符串传递或创建为每种类型的我想使用独立TRY_CONVERT方法“TARGET_TYPE”?