Solved

How can I pick this with sql?

Posted on 2015-02-17
5
201 Views
Last Modified: 2015-02-18
hi,

I have table where in one field the data is like

{name,100}
{name,101}
{name,102}
{secondname,200} {name,100}
{name,103}
{name,104}

so my goal is to pick 100 from here but when I use like bellow:

100
101
102
103
104

how can I do this with sql... i'm using postgress v8
0
Comment
Question by:hi4ppl
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Mark Bullock
ID: 40615936
Here's a way to get the result you want. You can play with this at SQL Fiddle http://sqlfiddle.com/#!11/8c057/5

CREATE TABLE mytable 
	(
     json varchar(30)
    );

INSERT INTO mytable
(json)
VALUES
('{name,100}'),
('{name,101}'),
('{name,102}'),
('{secondname,200} {name,100}'),
('{name,103}'),
('{name,104}');

select substring(json from 7 for 3)
from mytable
where json like '{name%';

Open in new window

0
 
LVL 1

Author Comment

by:hi4ppl
ID: 40615946
sorry I did mistake in providing the sample data well... here is the corrected one...

{name,10}
{name,101}
{name,10002}
{secondname,200} {name,1003}
{name,104}
{name,1005}

as the length of placement is not fixed ...plus thee placement of name sometimes moves....so the result that i'm looking for is :

 10
101
1002
1003
104
1005

thanks
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40615966
this would be far simpler in version 9.x

A way:
select
       replace(substring(thatpart,commapos+1), '}','') as picked
     , thatfield
from (
      select
             thatfield
           , split_part(thatfield, '} {', c+1) thatpart
           , strpos(split_part(thatfield, '} {', c+1), ',') commapos
      from (
            select
                    thatfield
                  , ((length(thatfield) - length(replace(thatfield,'} {',''))) / 3) as c
            from Table1
          ) x
     ) y
;

Open in new window


Or, without the nesting:
select
  replace(substring(split_part(thatfield, '} {', ((length(thatfield) - length(replace(thatfield,'} {',''))) / 3)+1),strpos(split_part(thatfield, '} {', ((length(thatfield) - length(replace(thatfield,'} {',''))) / 3)+1), ',')+1), '}','') as picked
, thatfield
from table1
;

Open in new window


Results:
| PICKED |                    THATFIELD |
|--------|------------------------------|
|     10 |                    {name,10} |
|    101 |                   {name,101} |
|  10002 |                 {name,10002} |
|   1003 | {secondname,200} {name,1003} |
|    104 |                   {name,104} |
|   1005 |                  {name,1005} |

Open in new window


see it at: http://sqlfiddle.com/#!11/90cff/2
0
 
LVL 1

Author Comment

by:hi4ppl
ID: 40615970
Hi,

thank you very much it looks good... but is there a way I can simplify this as I want to sometimes sum(thatfield) ...

regards
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40615979
Good luck with that simplification (note: if I could have I would have).

I don't use PostgreSQL every day, so there might be better ways I don't know about, and the job would be really simple if REVERSE() existed in 8.x (it's available in 9.x)

Plus you would have to add the necessary conversion to a number to perform a SUM(), and on top of that ensure you are only dealing with strings that can be converted to numbers.

Thus: I don't think so, but I could be wrong.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now