|
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.Read)]
public static SqlBoolean TryParseFn(string value, string dataType)
{
bool status = TryParse(value, dataType);
return new SqlBoolean(status);
}
/// <summary>
/// Function: TryParse
/// Purpose: Converts a string value to the target type. If succeeds returns true else false
/// </summary>
/// <param name="val"></param>
/// <param name="key"></param>
/// <returns>bool</returns>
private static bool TryParse(string val, string key)
{
bool flag = true;
Dictionary<string, System.Type> dict = new Dictionary<string, System.Type>();
dict.Add("int", typeof(Int32));
dict.Add("bigint", typeof(Int64));
dict.Add("datetime", typeof(DateTime));
dict.Add("numeric", typeof(Decimal));
try
{
switch (dict[key].FullName)
{
case "System.Int32":Int32.Parse(val);break;
case "System.Int64":Int64.Parse(val); break;
case "System.DateTime":DateTime.Parse(val); break;
case "System.Decimal": Decimal.Parse(val); break;
}
}
catch
{
flag = !flag;
}
return flag;
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.