I have a string
str="10*12*14"
now I want the total of this value
i.e. 10 multiply 12 multiply 14
how do i get it ?
is there any function in vb.net to do that ??
if i put it as query to database I get the result, but I want to do it in
the code.
vips 11 2495
Parse the string with String.Split() method, convert every substring to
integer with System.Convert.ToInt32() method (or Int32.Parse()) and multiply
the integers.
Eliyahu
"vips" <yp**@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... I have a string str="10*12*14" now I want the total of this value i.e. 10 multiply 12 multiply 14 how do i get it ?
is there any function in vb.net to do that ?? if i put it as query to database I get the result, but I want to do it in the code.
vips
thanks Eliyahu ,
I was wondering can i do this in sql query ?
I wrote it previously that it can be done using query, but It was my
mistake.
I mean is there any function in sql server ??
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:OI**************@TK2MSFTNGP15.phx.gbl... Parse the string with String.Split() method, convert every substring to integer with System.Convert.ToInt32() method (or Int32.Parse()) and
multiply the integers.
Eliyahu
"vips" <yp**@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... I have a string str="10*12*14" now I want the total of this value i.e. 10 multiply 12 multiply 14 how do i get it ?
is there any function in vb.net to do that ?? if i put it as query to database I get the result, but I want to do it
in the code.
vips
yes, sure
select 10*12*14
Eliyahu
"vips" <yp**@hotmail.com> wrote in message
news:eT****************@TK2MSFTNGP12.phx.gbl... thanks Eliyahu , I was wondering can i do this in sql query ? I wrote it previously that it can be done using query, but It was my mistake. I mean is there any function in sql server ??
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:OI**************@TK2MSFTNGP15.phx.gbl... Parse the string with String.Split() method, convert every substring to integer with System.Convert.ToInt32() method (or Int32.Parse()) and multiply the integers.
Eliyahu
"vips" <yp**@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... I have a string str="10*12*14" now I want the total of this value i.e. 10 multiply 12 multiply 14 how do i get it ?
is there any function in vb.net to do that ?? if i put it as query to database I get the result, but I want to do it in the code.
vips
> is there any function in vb.net to do that ??
No. However, you CAN use VB.Net to do that. All you have to do is write the
code.
Of course, first you have to analyze the problem. Programming is a
problem-solving process. Remember when you took Algebra in high school, and
they gave you word problems that you had to convert to math problems? Think
of it that way. Analysis is the process of converting a word problem to a
math (programming) problem. I have a string str="10*12*14" now I want the total of this value i.e. 10 multiply 12 multiply 14 how do i get it ?
Okay, first we start with a string. A string has no numbers in it. We want
to parse the string, as it contains text symbols that represent both data
(numbers) and process (math operators). Now, not all text is symbolic of
numbers and math operators, such as the comma for example. So, we first need
to identify what symbols of text we will use as numbers (the easy part -
0,1,2,3,4,5,6,7,8,9), and what symbols of text we will use as operators. I
cannot assume that you are only talking about multiplying numbers here. In
fact, I would guess that you may want to be able to perform all types of
arithmetical computations, IOW adding, subtracting, multiplying, and
dividing. So, for the purpose of this explanation, I'll say that our
operators are +, -, *, and /. Possibly % and \ as well.
Okay, now for the hard part. Your example is good because it contains no
spaces. We can't necessarily assume that there are spaces in the string (you
could, but I can't, since I don't know where your string will come from).
So, we need a means to parse a string into an unknown number of known single
characters, and an unknown number of combinations of 1 or more digit
characters. What is the best programmatic mechanism for doing such complex
string parsing? Offhand, I would say a Regular Expression. So, we have
solved the first part of the problem, which is how to parse the string into
a set of separate strings.
Once we have these separate strings, we must convert the numeric string
expressions to numbers, and the operators to operators, and build a purely
mathematical expression from them. Converting strings to numbers is easy,
and I don't need to explain that. Converting the operators cannot be done
directly, so we need to use selection to identify the operators, in order to
perform math on the numbers. This can be done with a Select Case Statement.
Now we need to know the rules of math, and for arithmentic they are fairly
simple. In this case, the simplicity comes in handy. We know, for example,
that (10*2*4) is equivalent to (10*2) * 4, and is equivalent to 10 * (2*4),
etc. If all we are doing is multiplying, the order of operation is not
important. But, what if we parse 10*2+4? 10*2 = 20. Add 4 and you get 24.
But 10 * (2+4) = 60. So, we're looking for a rule, and we introduce another
possible set of characters that we may need to parse, which is parentheses.
We need to identify whether we will allow the use of parentheses to change
the default rules regarding order of operation, and if so, we need to parse
parentheses, and use them in our math, not directly, but in order to perform
the operations in the correct order. Again, we're talking about selection
here. If there are no parentheses, the default order of operations applies.
If there ARE parentheses, we have to modify the order of operations, as a
program can only perform one operation at a time.
Now that we have analyzed the problem, it's a relatively straightforward
exercise to write the code. And I'll leave that to you!
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
"vips" <yp**@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...I have a string str="10*12*14" now I want the total of this value i.e. 10 multiply 12 multiply 14 how do i get it ?
is there any function in vb.net to do that ?? if i put it as query to database I get the result, but I want to do it in the code.
vips
Thinking it over, as I often do, I have one correction, and one additional
note.
Correction: I should have said 10*12*4, rather than 10*2*4. Same rules, but
sloppy on my part.
Additional note: You MAY need to identify if a string is unparsable, and
handle the exception. MOST strings can NOT be parsed into mathematical
expressions. If, for example, you write a SQL query that is supposed to
perform a mathematical operation, and it can not be parsed, the database
will throw an exception. So, depending upon how you're getting your string
expression, you may need to handle the eventuality that an unparsable string
is entered.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
"vips" <yp**@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... I have a string str="10*12*14" now I want the total of this value i.e. 10 multiply 12 multiply 14 how do i get it ?
is there any function in vb.net to do that ?? if i put it as query to database I get the result, but I want to do it in the code.
vips
> I mean is there any function in sql server ??
SELECT 10*12*4
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
"vips" <yp**@hotmail.com> wrote in message
news:eT****************@TK2MSFTNGP12.phx.gbl... thanks Eliyahu , I was wondering can i do this in sql query ? I wrote it previously that it can be done using query, but It was my mistake. I mean is there any function in sql server ??
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:OI**************@TK2MSFTNGP15.phx.gbl... Parse the string with String.Split() method, convert every substring to integer with System.Convert.ToInt32() method (or Int32.Parse()) and multiply the integers.
Eliyahu
"vips" <yp**@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... > I have a string > str="10*12*14" > now I want the total of this value > i.e. 10 multiply 12 multiply 14 > how do i get it ? > > is there any function in vb.net to do that ?? > if i put it as query to database I get the result, but I want to do it in > the code. > > vips > > > > > > >
I actually meant if I am having this string in database in one column
how can I process it ?
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:O8**************@TK2MSFTNGP14.phx.gbl... yes, sure
select 10*12*14
Eliyahu
"vips" <yp**@hotmail.com> wrote in message news:eT****************@TK2MSFTNGP12.phx.gbl... thanks Eliyahu , I was wondering can i do this in sql query ? I wrote it previously that it can be done using query, but It was my mistake. I mean is there any function in sql server ??
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:OI**************@TK2MSFTNGP15.phx.gbl... Parse the string with String.Split() method, convert every substring
to integer with System.Convert.ToInt32() method (or Int32.Parse()) and multiply the integers.
Eliyahu
"vips" <yp**@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... > I have a string > str="10*12*14" > now I want the total of this value > i.e. 10 multiply 12 multiply 14 > how do i get it ? > > is there any function in vb.net to do that ?? > if i put it as query to database I get the result, but I want to do
it in > the code. > > vips > > > > > > >
Well, in that case, refer to my first (long) answer. You would have to parse
the string as I described, and use the logic that I described.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
"vips" <yp**@hotmail.com> wrote in message
news:eK***************@TK2MSFTNGP15.phx.gbl... I actually meant if I am having this string in database in one column how can I process it ?
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message news:O8**************@TK2MSFTNGP14.phx.gbl... yes, sure
select 10*12*14
Eliyahu
"vips" <yp**@hotmail.com> wrote in message news:eT****************@TK2MSFTNGP12.phx.gbl... > thanks Eliyahu , > I was wondering can i do this in sql query ? > I wrote it previously that it can be done using query, but It was my > mistake. > I mean is there any function in sql server ?? > > > "Eliyahu Goldin" <re*************@monarchmed.com> wrote in message > news:OI**************@TK2MSFTNGP15.phx.gbl... > > Parse the string with String.Split() method, convert every substring to > > integer with System.Convert.ToInt32() method (or Int32.Parse()) and > multiply > > the integers. > > > > Eliyahu > > > > "vips" <yp**@hotmail.com> wrote in message > > news:%2****************@TK2MSFTNGP12.phx.gbl... > > > I have a string > > > str="10*12*14" > > > now I want the total of this value > > > i.e. 10 multiply 12 multiply 14 > > > how do i get it ? > > > > > > is there any function in vb.net to do that ?? > > > if i put it as query to database I get the result, but I want to do it > in > > > the code. > > > > > > vips > > > > > > > > > > > > > > > > > > > > > > > > > > >
I think, you can use “EXEC” command to run a query string in SQL 2000.
Something likes that:
CREATE PROCEDURE MySoredProcedure
AS
DECLARE @lv_SQL VARCHAR(1000)
SET @lv_SQL = 'SELECT’ + ’10*12*4’
EXEC(@lv_SQL)
GO
Shaw
"Kevin Spencer" wrote: Thinking it over, as I often do, I have one correction, and one additional note.
Correction: I should have said 10*12*4, rather than 10*2*4. Same rules, but sloppy on my part.
Additional note: You MAY need to identify if a string is unparsable, and handle the exception. MOST strings can NOT be parsed into mathematical expressions. If, for example, you write a SQL query that is supposed to perform a mathematical operation, and it can not be parsed, the database will throw an exception. So, depending upon how you're getting your string expression, you may need to handle the eventuality that an unparsable string is entered.
-- HTH,
Kevin Spencer Microsoft MVP ..Net Developer What You Seek Is What You Get.
"vips" <yp**@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...I have a string str="10*12*14" now I want the total of this value i.e. 10 multiply 12 multiply 14 how do i get it ?
is there any function in vb.net to do that ?? if i put it as query to database I get the result, but I want to do it in the code.
vips
Shaw has the right idea.
Here's what I'd do if you're going to keep using this same stored procedure
to compute values based on a column that has your equation to compute.
-- start of stored procedure
CREATE PROCEDURE ProcessTotals
@computevalue varchar(100)
as
declare @s varchar(100)
set nocount on
select 0 as Ttl into #t
delete #t
set @s = 'insert into #t (Ttl) values (' + @computevalue + ')'
exec (@s)
select Ttl from #t
drop table #t
-- end of stored procedure
So you'd call your ProcessTotals stored procedure as follows:
exec ProcessTotals '10*12*14'
or
declare @s varchar(100)
set @s = '10*12*14'
exec ProcessTotals @s
or call it from VB or C# or ?? code.
I think you get the idea
This does work, I made sure to test it.
One thing to note, if @computevalue is not a valid equation this will fail.
If you're calling this stored procedure from code be sure to check for any
errors.
The only thing with Shaw's approach in a real world application, users or
code are not allowed to create stored procedures on the fly. To allow that
is to allow possible security risks.
"Shaw" <Sh**@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com... I think, you can use "EXEC" command to run a query string in SQL 2000. Something likes that:
CREATE PROCEDURE MySoredProcedure AS DECLARE @lv_SQL VARCHAR(1000) SET @lv_SQL = 'SELECT' + '10*12*4' EXEC(@lv_SQL) GO
Shaw
"Kevin Spencer" wrote:
Thinking it over, as I often do, I have one correction, and one additional note.
Correction: I should have said 10*12*4, rather than 10*2*4. Same rules, but sloppy on my part.
Additional note: You MAY need to identify if a string is unparsable, and handle the exception. MOST strings can NOT be parsed into mathematical expressions. If, for example, you write a SQL query that is supposed to perform a mathematical operation, and it can not be parsed, the database will throw an exception. So, depending upon how you're getting your string expression, you may need to handle the eventuality that an unparsable string is entered.
-- HTH,
Kevin Spencer Microsoft MVP ..Net Developer What You Seek Is What You Get.
"vips" <yp**@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... >I have a string > str="10*12*14" > now I want the total of this value > i.e. 10 multiply 12 multiply 14 > how do i get it ? > > is there any function in vb.net to do that ?? > if i put it as query to database I get the result, but I want to do it > in > the code. > > vips > > > > > > >
Darn, Shaw, I think you finally understood the question! How could I have
missed it?
--
Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
"Shaw" <Sh**@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com... I think, you can use "EXEC" command to run a query string in SQL 2000. Something likes that:
CREATE PROCEDURE MySoredProcedure AS DECLARE @lv_SQL VARCHAR(1000) SET @lv_SQL = 'SELECT' + '10*12*4' EXEC(@lv_SQL) GO
Shaw
"Kevin Spencer" wrote:
Thinking it over, as I often do, I have one correction, and one additional note.
Correction: I should have said 10*12*4, rather than 10*2*4. Same rules, but sloppy on my part.
Additional note: You MAY need to identify if a string is unparsable, and handle the exception. MOST strings can NOT be parsed into mathematical expressions. If, for example, you write a SQL query that is supposed to perform a mathematical operation, and it can not be parsed, the database will throw an exception. So, depending upon how you're getting your string expression, you may need to handle the eventuality that an unparsable string is entered.
-- HTH,
Kevin Spencer Microsoft MVP ..Net Developer What You Seek Is What You Get.
"vips" <yp**@hotmail.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl... >I have a string > str="10*12*14" > now I want the total of this value > i.e. 10 multiply 12 multiply 14 > how do i get it ? > > is there any function in vb.net to do that ?? > if i put it as query to database I get the result, but I want to do it > in > the code. > > vips > > > > > > > This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Eugene Druker |
last post by:
Hi all,
I need eigen values and vectors for symmetric matrices (like VCV).
Solving with numarray and testing the results, I've got
strange results - input and output matrices of some sizes are...
|
by: homa |
last post by:
Hi . My problem is that I have XML provided to me by URL but there are
no ID tags. How do I loop through and find a specific value in the XML?
The site I'm working on uses classic ASP. Here is my...
|
by: Senna |
last post by:
Hi
Have these to work with.
int counter = 0; //The total item to fill the collection with
int increment = 2; //Any number
int current = 244; //Quantity
int max = 1290; //Max Quantity
int...
|
by: merriebeth |
last post by:
I am trying to round up a value using an expression to nearest 5. Here is what I would like:
10: 10
12: 15
14: 15
15: 15
Tried Round (x,5)
Would also like to multiply by 30%.
|
by: vsraot |
last post by:
Hi,
How to dispaly two dicimal points in java (Example 1) 5.70 Example 2) 561 but it should display as 561.00)
Example1) Here I am able to display 5.7 but it should display as 5.70
Example...
|
by: PHPSplashBoard |
last post by:
PHP resource site update news letter 02-12-2007
- Job section is completely rewritten and now the site shows PHP
jobs
from various job portals. PHP jobs...
|
by: vijay1012 |
last post by:
I am using a script. It autometically updates some cases. And it updates history of the cases also. But i don't want to be like that. I don't wnat to update history.
Is there any way to do.
|
by: Kapteyn's Star |
last post by:
Hi newsgroup
The program here given is refused by GCC with a error i cannot
understand. It says
rnd00.c: In function ‘main’:
rnd00.c:26: error: expected expression before ‘]’ token
...
|
by: risk32 |
last post by:
Hello all. As I am just a sapling according to the world of programming, I've come across what I thought would have been sufficient at the task I needed. Only problem... inter-twining Javascript and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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...
| |