0

I am wonder if i can extract multiple data with a single query using string operations.?

Here is what i am looking for My query returns me

+-----------------------------------------+
|               geodata                   |
+-----------------------------------------+
| {"lat"=>"28.644348", "lon"=>"77.219682" |
+-----------------------------------------+

Can i extract lat value and lon value through any string query?

i tried and succeed up to extracting a single value.

can anyone suggest me how to do this?

of-course i can do this after pulling this data to my rails app but i am thinking to pull this data directly from the query.

2
  • show us what you tried and we can go from there. Commented Nov 27, 2014 at 13:16
  • select replace(substring_index('{"lat"=>"28.644348", "lon"=>"77.219682" ','"',-2),'"',''); Commented Nov 27, 2014 at 13:28

1 Answer 1

1

Yes it could be done assuming that the pattern you shown is same

mysql> select replace(substring_index(substring_index('{"lat"=>"28.644348", "lon"=>"77.219682"','"lat"=>',-1),',',1),'"','') as lat;
+-----------+
| lat       |
+-----------+
| 28.644348 |
+-----------+
1 row in set (0.00 sec)

mysql> select replace(substring_index(substring_index('{"lat"=>"28.644348", "lon"=>"77.219682"','"lon"=>',-1),',',1),'"','') as lon;
+-----------+
| lon       |
+-----------+
| 77.219682 |
+-----------+

So finally you can combine both of them into a single select

select 
replace(substring_index(substring_index(geodata,'"lat"=>',-1),',',1),'"','') as lat,
replace(substring_index(substring_index(geodata,'"lon"=>',-1),',',1),'"','') as lon
from your_table
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.