Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I pick this with sql?

Posted on 2015-02-17
5
Medium Priority
?
267 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 14

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
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
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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