Solved

How can I pick this with sql?

Posted on 2015-02-17
5
228 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to recover a database from a user managed backup

713 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