Click here to Skip to main content
Rate this: bad
good
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 1:28am
Comments
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 server.like the above question you have to get answer 2500 but how becoz we have '25*100' in string format.

1 solution

Rate this: bad
good
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:
SELECT 
 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)
 
Addition:
 
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.
  Permalink  
v2
Comments
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 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 1 Sep 2012
Copyright © CodeProject, 1999-2014
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