Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9260369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:52:28+00:00 2026-06-18T12:52:28+00:00

I am using the collect function to concatenate strings for a sql query. select

  • 0

I am using the collect function to concatenate strings for a sql query.

    select id,
    tab_to_string(CAST(COLLECT(a.level||' '||d.Number||':  
    '||to_char(nvl(de.eventDate,SYSDATE - 365 * 100))) AS t_varchar2_tab)) AS MyVar
    from Mytable
    groupby id

The output of this query is like:

    Id    Myvar
    1     level : 27-Jan-09,level : 27-Mar-08, level : 2-Apr-10
    2     level : 7-Jun-06,level : 27-Dec-08, level : 2-Nov-08
    3     level : 27-July-10,level : 27-Mar-06, level : 2-Apr-10

But i want the "Myvar" value to be ordered by the date field within the concatenated string

so for the Id = 1, the output should be like

    level : 27-Mar-08, level : 27-Jan-09, level : 2-Apr-10

Below is the code for tab_to_string function

source: http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php#wm_concat

    CREATE OR REPLACE FUNCTION tab_to_string (p_varchar2_tab  IN  t_varchar2_tab,
                                      p_delimiter     IN  VARCHAR2 DEFAULT ',') 
    RETURN VARCHAR2 IS
    l_string     VARCHAR2(32767);
    BEGIN
    FOR i IN p_varchar2_tab.FIRST .. p_varchar2_tab.LAST LOOP
    IF i != p_varchar2_tab.FIRST THEN
    l_string := l_string || p_delimiter;
    END IF;
    l_string := l_string || p_varchar2_tab(i);
    END LOOP;
    RETURN l_string;
    END tab_to_string;

I am using Oracle 10g.

Thanks
Prash

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:52:29+00:00Added an answer on June 18, 2026 at 12:52 pm

    to get an ordered list, there’s a few ways. the simplest is :

    select id, str
      from (select id, 
                   wm_concat('level : ' || to_char(nvl(eventDate,SYSDATE - 365 * 100))) 
                     over (partition by id order by eventdate) str,
                   row_number() over (partition by id order by eventdate desc) rn
             from Mytable)
     where rn = 1;
    

    or if you’re using the “stragg” user defined aggregate:

      select id, str
      from (select id, 
                   string_agg('level : ' || to_char(nvl(eventDate,SYSDATE - 365 * 100))) 
                     over (partition by id order by eventdate) str,
                   row_number() over (partition by id order by eventdate desc) rn
             from Mytable)
     where rn = 1;
    

    eg

    SQL> select id, str
      2    from (select id,
      3                 string_agg('level : ' || to_char(nvl(eventDate,SYSDATE - 365 * 100)))
      4                   over (partition by id order by eventdate) str,
      5                 row_number() over (partition by id order by eventdate desc) rn
      6           from Mytable)
      7   where rn = 1;
    
            ID STR
    ---------- ----------------------------------------------------------------------
             1 level : 27-MAR-08,level : 27-JAN-09,level : 02-APR-10
             2 level : 07-JUN-06,level : 02-NOV-08,level : 27-DEC-08
             3 level : 27-MAR-06,level : 02-APR-10,level : 27-JUL-10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When using cast(collect(...)), how do I order the result? I have a function called
I am trying to find a way to collect data using sql aggregate functions
I am currently using the query below to collect a total of the ER_read
Currently, I am using this function get indicies to collect the key/values from an
I created a little web spider in Python which I'm using to collect URLs.
I am using StringDictionary collection to collect Key Value Pairs. E.g.: StringDictionary KeyValue =
We are using a FormAssembly page to collect feedback - it's hosted within an
I'm using PHP to scrape a website and collect some data. It's all done
We're using xVal and the standard DataAnnotationsValidationRunner described here to collect validation errors from
While tracing my modules using dbg, I encountered with the problem how to collect

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.