SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Calculate an expression


Calculate an expression

Author
Message
sistemas_casinomnes
sistemas_casinomnes
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 125
Hello, I need help to calculate an expression in sql.
For example I have to send (248/5)*30 to a store procedure or to a function and the sp or the function has to return me the result, in this case is 1488.
Kingston Dhasian
Kingston Dhasian
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6225 Visits: 5291
You can use Dynamic SQL to handle the scenario
But, be really careful about SQL Injection and take necessary steps to avoid it

CREATE PROCEDURE dbo.usp_ShowExpressionResults
(
@Expression NVARCHAR(MAX)
)

AS

BEGIN

SET @Expression = 'SELECT ' + @Expression + ' AS Result'

EXECUTE sp_executesql @Expression

END




Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)

Group: General Forum Members
Points: 54756 Visits: 21216
sistemas_casinomnes (2/26/2013)
Hello, I need help to calculate an expression in sql.
For example I have to send (248/5)*30 to a store procedure or to a function and the sp or the function has to return me the result, in this case is 1488.


Can you explain why the calculation has to be done in SQL?

Here is one way. Create three variables in your package:

1) Expression. A String which will contain the expression to be evaluated, eg (248/5)*30 in this case.
2) Result. A Single which will contain the result.
3) SQLExpression a String which is a calculated expression. Set the Expression as follows:

"select " + @[User::Expression] + " Result"

Notice that, if you click on Evaluate Expression, the result will be as follows:

select (248/5)*30 Result

Which is the query which will give you your result in T-SQL.

Now create an ExecuteSQL task. Define the connection to your SQL Server instance and set SQLSourceType to variable. Set the source variable to be User::SQLExpression and set ResultSet to SingleRow.

Now move to the Result Set section of the ExecuteSQL task and click on the Add button. Set Result Name to 'Result' and Variable name to User::Result.

Done. When the ExecuteSQL task runs it will execute the SQL and store the result in 'Result'.

Note that in this case, the result will be 1470, not 1488, as your expression will be performed using integer arithmetic.

You can force T-SQL to do the calculation using real arithmetic by using something like this instead: (248.0 / 5) * 30


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
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