Solved

How can I pick this with sql?

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Character matching different date formats for dates between 6 59
Mongo DB 18 62
Oracle Insert not working 10 32
Oracle Distributed Transaction Lock Error ORA-01591 8 52
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.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
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…

832 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