Click here to Skip to main content
15,911,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Select '25*100'


how to solve it in postgresql.
Posted
Comments
ZurdoDev 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 server.like the above question you have to get answer 2500 but how becoz we have '25*100' in string format.

1 solution

One way to do this is to split the data based on the operator. For example to multiply, perhaps something like:
SQL
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.
 
Share this answer
 
v2
Comments
Rohit rgdon 1-Sep-12 0:54am    
it is not working in postgres
Wendelius 1-Sep-12 1:24am    
What error do you get?
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'.
Wendelius 1-Sep-12 1:23am    
Yes, I know. That's why I mentioned that this would only be for a multiply :)
[no name] 1-Sep-12 1:06am    
u can use a variable when u dont have fix value

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900