Click here to Skip to main content
11,705,212 members (51,807 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: PostgreSQL query
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 Sergey Alexandrovich Kryukov 525
1 OriginalGriff 244
2 ppolymorphe 126
3 F-ES Sitecore 110
4 Andy Lanng 85
0 OriginalGriff 8,988
1 Sergey Alexandrovich Kryukov 8,272
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web04 | 2.8.150819.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