Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Eval arithmetic statement function in SQL


Eval arithmetic statement function in SQL

Author
Message
Bharat Panthee
Bharat Panthee
SSC-Addicted
SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)

Group: General Forum Members
Points: 446 Visits: 162
Comments posted to this topic are about the item Eval arithmetic statement function in SQL
Rajganesh Mountbatton
Rajganesh Mountbatton
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 32
Why complicating when a simple solution is available?


Create Function EvalSQLStatement(@strStatement as varchar(1000))
returns decimal(12,2)
As
Begin
Return Exec('Select' + @strStatement)
End



Exec('Select' + @strStatement) will evaluate the expression and give its results.
Bharat Panthee
Bharat Panthee
SSC-Addicted
SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)SSC-Addicted (446 reputation)

Group: General Forum Members
Points: 446 Visits: 162
i get error 'RETURN statements in scalar valued functions must include an argument.'
when try to create to that function.
Rajganesh Mountbatton
Rajganesh Mountbatton
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 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. Sad

But the solutions remains intact. You can use EXEC('Select' + @strStatement) to evaluate an expression in your queries.
vevoda.ulath
vevoda.ulath
SSC-Enthusiastic
SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)SSC-Enthusiastic (176 reputation)

Group: General Forum Members
Points: 176 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...
Jeferson Alexandre Borelli Bernardo
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 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
Iwas Bornready
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8648 Visits: 885
Thanks for the script.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search