Click here to Skip to main content
15,895,011 members
Articles / Database Development / SQL Server / SQL Server 2008

An Introduction to Sql 11 (Code Name Denali) –Part VI (T-Sql Features in CTP 3)

Rate me:
Please Sign up or sign in to vote.
4.68/5 (12 votes)
1 Sep 2011CPOL29 min read 35.5K   151   20  
In this article we will explore on the new features that Denali CTP 3 has offer us from a T-Sql perspective
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions