## Eval arithmetic statement function in SQL

 Author Message Bharat Panthee Right there with Babe Group: General Forum Members Points: 740 Visits: 162 Comments posted to this topic are about the item Eval arithmetic statement function in SQL Rajganesh Mountbatton SSC Journeyman Group: General Forum Members Points: 91 Visits: 32 Why complicating when a simple solution is available?`Create Function EvalSQLStatement(@strStatement as varchar(1000))returns decimal(12,2)AsBegin Return Exec('Select' + @strStatement)End`Exec('Select' + @strStatement) will evaluate the expression and give its results. Bharat Panthee Right there with Babe Group: General Forum Members Points: 740 Visits: 162 i get error 'RETURN statements in scalar valued functions must include an argument.'when try to create to that function. Rajganesh Mountbatton SSC Journeyman Group: General Forum Members Points: 91 Visits: 32 Oh yes. There is a limitation in SQL Server. We can't use Exec inside UDF. EXEC('Select' + @strStatement) can't be used inside your function. But the solutions remains intact. You can use EXEC('Select' + @strStatement) to evaluate an expression in your queries. vevoda.ulath SSC Veteran Group: General Forum Members Points: 294 Visits: 396 Well, not really useful, since it doesn't support brackets.Draft send by Rajganesh on the other hand doesn't support decimals. (of course, I can force it)Don't have time right now to provide mine solution, but for this script to be useful it should works as follows:`select dbo.EvalSQLStatement('(5 * 3 + 1) / 3')-- result should be 5.333333` Jeferson Alexandre Borelli... Valued Member Group: General Forum Members Points: 64 Visits: 28 You dont need tocreate a function.A easier way using only EXEC :EXEC('SELECT CAST( (1.66 * 300 / 1170) + (1.75 * 220 / 1170) + (1.791 * 0 / 1170) + (1.6666 * 100 / 1170) + (1.96 * 350 / 1170) + (1.8716 * 100 / 1170) + (1.6666 * 0 / 1170) + (1.44 * 100 / 1170) AS DECIMAL(18,4))') Iwas Bornready One Orange Chip Group: General Forum Members Points: 29334 Visits: 885 Thanks for the script.