Solved

How can I pick this with sql?

Posted on 2015-02-17
5
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 49

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 49

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

Technology Partners: 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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

728 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