473,498 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

retrieving a string into mathematical expression

13 New Member
Good Day!!!

Is it somehow possible to retrieve a string and then convert it into a mathematical expression? for example i have this string '14200 * 5' and then i want to convert this into a mathematical formula so that i could get the result of it which is 71000

Thanks....
Oct 25 '07 #1
7 11294
Jim Doherty
897 Recognized Expert Contributor
Good Day!!!

Is it somehow possible to retrieve a string and then convert it into a mathematical expression? for example i have this string '14200 * 5' and then i want to convert this into a mathematical formula so that i could get the result of it which is 71000

Thanks....
The Function EVAL is what you need to look at ie:

ie: myvalue=Eval('14200*5')

assigns the value 71000 to the variable myvalue in vba code

you can also use the Eval function in SQL

ie: SELECT Eval('14200*5') AS Blah;

will return you a single column called blah in which contains a single row value of 71000

There are obviously any number of ways you may wish to use the return value either in SQL or VBA code

Regards

Jim
Oct 25 '07 #2
clear1140
13 New Member
i tried to use the eval function but it seems that MSSQL does not recognize the function
Oct 25 '07 #3
Jim Doherty
897 Recognized Expert Contributor
i tried to use the eval function but it seems that MSSQL does not recognize the function
Apologies I posted this reply thinking it was the Access forum didnt realise it was the MS SQL forum ..there you are we all make stupid mistakes LOL I flick back and forth between them like tennis :)

Jim :)
Oct 25 '07 #4
clear1140
13 New Member
Apologies I posted this reply thinking it was the Access forum didnt realise it was the MS SQL forum ..there you are we all make stupid mistakes LOL I flick back and forth between them like tennis :)

Jim :)
Apologies accepted.... but still i dont know what function to use... :(
Oct 25 '07 #5
Jim Doherty
897 Recognized Expert Contributor
Apologies accepted.... but still i dont know what function to use... :(
There isnt an EVAL function in Sql server like there is in Access it might well be a wish list thing. In the absence of it you could maybe workaround with something like this stored procedure accepting a string parameter and executing a simple select to return a single row with the data value being the result of your parameter passed in?

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE PROCEDURE dbo_usp_Eval
  3. (@mystr varchar(4000))
  4. AS
  5. SET NOCOUNT ON
  6. DECLARE @SQLString NVARCHAR(4000)
  7. SET @SQLString = 'SELECT '+@mystr
  8. EXEC sp_executesql @SQLString
  9. GO
  10.  
a typical execution of this might be this from query analyser or an application

EXEC dbo_usp_Eval '14200 * 5'

it depends largely on how you are dealing with your data and what you are actually trying to do. If you are thinking of a user defined function to use in a view as part of a SELECT as a column then I think you're out of luck? unless somelse comes up with something

Regards

Jim :) :(
Oct 25 '07 #6
Vanaspati Bhavsar
1 New Member
Hi

Instead of using select Eval('14000*5')

use

exec('select 14200*5') in sql

I think this should work

All the best
Oct 22 '08 #7
Daeku
1 New Member
Disclaimer: I'm the owner of the project Eval SQL.NET on GitHub

Starting from SQL Server 2012, it's possible to evaluate arithmetic/mathematical expression in T-SQL using this SQL CLR.

Expand|Select|Wrap|Line Numbers
  1. -- Without parameter
  2. DECLARE @result1 INT = SQLNET::New('14200*5').EvalInt()
  3. -- With parameter
  4. DECLARE @result2 INT = SQLNET::New('X*Y').Val('X', 14200).Val('Y', 5).EvalInt()
  5.  
  6. -- SELECT 71000, 71000
  7. SELECT @result1, @result2
Documentation: Dynamically evaluate mathematical expression
Feb 11 '16 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2337
by: Jonas Prismesen | last post by:
Hi! I have string like this: string expr = "123/(12*3)"; And I want to actually calculate the mathematical expression in the string. Is there an easy way do it? Or do I have to extract all...
3
1442
by: stanlo | last post by:
hi to everyone, this is still a follow up of my project ,mathematical expression.this project is meant to evaluate mathemtical expressions with oparators,+,-,*,/.more than two operands can be done,...
3
5184
by: tobimarq | last post by:
hi all, I wonder if the following is possible in php: I want php to 'interpret' a string that contains (a simple kind of) mathematical calculation, like $var='25*3'; $result= (int) $var;...
22
2046
by: jacob navia | last post by:
A function like strcpy takes now, two unbounded pointers. Unbounded pointers, i.e. pointers where there is no range information, have catastrophic failure modes specially when *writing* to main...
13
33795
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this...
17
2012
by: Dinsdale | last post by:
I would like to compare a string value to a pre-determined list of other strings. Is there a simple way to do this in one statements like this: if(strMystring.ToUpper() == ("STRING1"| "STRING2"|...
9
2793
by: Bob M | last post by:
If I define an expression or equation, how can I retrieve that expression as string literal? I want to do this so that I could avoid repetitive typing (or copy/paste/change) the same thing at two...
4
2840
by: aalam | last post by:
Is there any function available in C/C++ libarary to integrate a mathematical expression. Thanks in advance....
13
2135
by: jacek.strzelczyk | last post by:
Hello, I'm looking for a C library that provides the notation of n- dimensional mathematical functions. Or is there any other way to decode that kind of functions in C language? Thanks in...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.