Friday, February 5, 2010

HOw to use user defined function in sql server 2005

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Data.SqlTypes;
///
/// Summary description for Reg_exp
///

public partial class Reg_exp
{
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true,IsPrecise=true)]
public static SqlString Reg_exp_function(SqlString expression, SqlString pattern, SqlString replace)
{
if (expression.IsNull || pattern.IsNull || replace.IsNull)
return SqlString.Null;

Regex r = new Regex(pattern.ToString());

return new SqlString(r.Replace(expression.ToString(), replace.ToString()));

}
}

NOW ACCESS THIS FUNCTION FROM SQL SERVER]


1) sp_configure 'clr enabled',1

2) reconfigure

3) select dbo.Reg_exp_function('Remove1All3Letters7','[a-zA-Z]','')

No comments:

Post a Comment