1

I have a column in my table having values of format:

COURSE_214/MODULE_5825/SUBMODULE_123/..../GOAL_124/ACTIVITY_125.

I need value for goal i.e 124 from goal_124. I am planning to use 'regexp_split_to_array' but don't know how to use elements from array.

I am using postgres 9.2.

3 Answers 3

1

You can use split_part like so:

select split_part(split_part('COURSE_214/MODULE_5825/SUBMODULE_123/..../GOAL_124/ACTIVITY_125', '/GOAL_', 2), '/', 1)
i.e.
select split_part(split_part(fieldname, '/GOAL_', 2), '/', 1)

Result:
124
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.... suppose there are multiple 'GOAL_' fields...then how to get it? json_object won't work for me as I am using postgres 9.2
1

Using json_object():

select json_object(string_to_array(translate(params, '_', '/'), '/'))
from test

                                          json_object                                           
------------------------------------------------------------------------------------------------
 {"COURSE" : "214", "MODULE" : "5825", "SUBMODULE" : "123", "GOAL" : "124", "ACTIVITY" : "125"}
(1 row)

select json_object(string_to_array(translate(params, '_', '/'), '/'))->>'GOAL' as goal
from test

 goal 
------
 124
(1 row) 

The column has a format suitable for json. I would suggest to change the type of the column to jsonb. The first query may be used as a converter. After the conversion you would access the parameters in an easy way, e.g.:

select *
from test
where params->>'COURSE' = '214'
and (params->>'GOAL')::int > 120;

Simple select of all GOAL_ parameters (if there are more than one)

select ltrim(elem, 'GOAL_')
from (
    select unnest(string_to_array(params, '/')) elem
    from test
    ) sub
where elem like 'GOAL_%'

6 Comments

That's a very nice use of json in combination with string_to_array and translate
This is not working for me. string_to_array() function is returning results of the form: {COURSE,249,MODULE,6312,GOAL,9059,ACTIVITY,8774,ACTIVITY,8800}. When I am passing it to json_object(), I am getting the error : ' function json_object(text[]) does not exist'
To use json_object() you need Postgres 9.3+ (for the jsonb type - 9.4+).
Ok... can you suggest how to do in postgres 9.2? we can have multiple GOAL_ in the string
In 9.2 you could use hstore. However my solution assumes unique keys. Perhaps it would be worthwhile to ask a new question with the detailed description of the parameters structure (Postgres version should be always included in a question).
|
0

You may try using regular expressions, getting the string between slashes

select substring(your_column from '^.*/(.*)/.*$') from your_table

If you expect to find in that part the GOAL value, use

select substring(your_column from '/GOAL_(.*)/') from your_table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.