Click here to Skip to main content
11,806,350 members (64,561 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: PostgreSQL
Select '25*100'

how to solve it in postgresql.
Posted 31-Aug-12 0:28am
ryanb31 at 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 at 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 at 1-Sep-12 0:54am
it is not working in postgres
Mika Wendelius at 1-Sep-12 1:24am
What error do you get?
Rohit rgdon at 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'.
Mika Wendelius at 1-Sep-12 1:23am
Yes, I know. That's why I mentioned that this would only be for a multiply :)
vijay bisht at 1-Sep-12 1:06am
u can use a variable when u dont have fix value
Mika Wendelius at 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 at 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
0 Richard Deeming 205
1 Sergey Alexandrovich Kryukov 170
2 ppolymorphe 145
3 OriginalGriff 135
4 Abhinav S 120
0 OriginalGriff 3,330
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,905
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 1 Sep 2012
Copyright © CodeProject, 1999-2015
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