473,618 Members | 3,044 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 11327
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('1 4200*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
2363
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 the parts from the string, which will be difficult since the expression will change in my app..
3
1453
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, eg it should be able to do,1+9-45*7/12,or 4* 5+6*21.from reading and help from you i have been able to write the program.but my problem is to process the string of the input.i.e given for example an input string, 12+6*65/7, you know i have to...
3
5192
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; //according to the php manual, $result contains 25, //but I want the result at the end to contain: 75 Is there a php function or construct, that does this?
22
2072
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 memory. A better string library would accept *bounded* pointers. We would have then: char *strcpyN(char *destination, size_t bound1, char *src,size_t bound2);
13
33833
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 should return 0 or false( ( 3 + 6 ) / 3 ) > ( ( 5 + 3 ) / 4 ) --> this should return 1 or trueThanks for any comment or
17
2024
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"| "STRINGX")) {} This syntax doesn't work (and in fact I'm not quite sure what this statement really is saying) but I'd like to be able to do *something* like this. One suggestion was to add the compare strings to an
9
2802
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 place. I try to demonstrate the problem in the following code. Bob M <%@ Page Language="C#" %> <script runat="server">
4
2847
by: aalam | last post by:
Is there any function available in C/C++ libarary to integrate a mathematical expression. Thanks in advance....
13
2148
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 advance, Jacek
0
8212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8304
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8455
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4065
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2587
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 we have to send another system
1
1760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.