Click here to Skip to main content
13,152,454 members (38,481 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Select '25*100'

how to solve it in postgresql.
Posted 31-Aug-12 0:28am
ryanb31 31-Aug-12 8:26am
I have not used postgresql so maybe I am missing something obvious but I don't understand what you are asking.
Rohit rgdon 31-Aug-12 8:40am
Just take it as a database .it is a question like you have to solve the question in query database may be an like sql the above question you have to get answer 2500 but how becoz we have '25*100' in string format.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

One way to do this is to split the data based on the operator. For example to multiply, perhaps something like:
 CAST( SUBSTRING('25*100' FROM 1 TO (POSITION('*' IN '25*100') - 1)) AS INTEGER)
 CAST( SUBSTRING('25*100' FROM (POSITION('*' IN '25*100') + 1) TO 9999999) AS INTEGER)


To clarify the situation, using a single string to do this is a bad idea. What actually would be required is to use a lexer to properly split the operations into correct order.

The example works only with a single multiply operation, nothing else.

So the overall solution should be to rethink the application design in order to handle the calculations in some different way.
Rohit rgdon 1-Sep-12 0:54am
it is not working in postgres
Rohit rgdon 1-Sep-12 1:00am
currently your solution is working.but if i don's know that string may be '25*100' or '5000+100/12' or '(5000/12)+10'.
vijay bisht 1-Sep-12 1:06am
u can use a variable when u dont have fix value
Mika Wendelius 1-Sep-12 1:23am
Yes, I know. That's why I mentioned that this would only be for a multiply :)
Mika Wendelius 1-Sep-12 1:24am
What error do you get?
Mika Wendelius 1-Sep-12 1:24am
Yes, but if you already have variables, there's no reason to handle them as a single string. Just multiply the variables.
Rohit rgdon 6-Sep-12 3:24am
Work is done,I make a function in postgres to handel this('5000+100').which accept this compute then return its numeric value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170924.2 | Last Updated 1 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100