Sql Function For Converting String Expression To Proper Value
I need a function to convert mathematical expressions to a float value: create function dbo.ExpressionToValue ( @expression nvarchar(max) ) returns float as begin declar
Solution 1:
An alternative hack is to leverage C# in the form of an assembly. C# does not have an eval function, but this too can be hacked.
You can create an assembly function like
using System.Data.DataTable
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
publicpartialclassUserDefinedFunctions
{
[SqlFunction()]
publicstaticdoubleeval(string expression)
{
System.Data.DataTable table = new System.Data.DataTable();
return Convert.ToDouble(table.Compute(expression, String.Empty));
}
}
Build the assembly using VisualStudio and then register the assembly. One word of caution is that you might have to compile down to a lower version of the .NET framework depending on which one is supported by your current version of SQL server.
You can register your assembly in SQL Server using the procedure described on MSDN https://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.100).aspx
Post a Comment for "Sql Function For Converting String Expression To Proper Value"