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 7786841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:29:30+00:00 2026-06-01T20:29:30+00:00

How to make function in postgres that would take string or array and return

  • 0

How to make function in postgres that would take string or array and return all combinations of some length?

For example you have ABC and you want to get combinations with 2 characters, the result shoul be:

AB
AC
BC

Thank you in advance for your help.

  • 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-01T20:29:32+00:00Added an answer on June 1, 2026 at 8:29 pm
    set search_path='tmp';
    
    WITH ztab AS (
    SELECT idx as idx
    , substring ( 'WTF!' FROM idx FOR 1) as str
    FROM generate_series(1, char_length( 'WTF!' )) idx
    )
    SELECT t1.str, t2.str
    FROM ztab t1
    JOIN ztab t2 ON t2.idx > t1.idx
            ;
    

    Result:

     str | str 
    -----+-----
     W   | T
     W   | F
     W   | !
     T   | F
     T   | !
     F   | !
    (6 rows)
    

    Unfortunately I cannot find a way to avoid the double string constant. (but the whole thing could be packed into a function) If there are no duplicate characters (or you want to suppres them) you could do the anti-join on the str instead of the idx.

    UPDATE (hint from ypercube) It appears that the OP wants the strings to be concatenated. So be it::

    WITH ztab AS (
    SELECT idx as idx
    , substring ( 'WTF!' FROM idx FOR 1) as str
    FROM generate_series(1, char_length( 'WTF!' )) idx
    )
    SELECT t1.str || t2.str AS results
    FROM ztab t1
    JOIN ztab t2 ON t2.idx > t1.idx
            ;
    

    Results:

     results 
    ---------
     WT
     WF
     W!
     TF
     T!
     F!
    (6 rows)
    

    UPDATE2: (here comes the recursive thingy…)

    WITH RECURSIVE xtab AS (
            WITH no_cte AS (
            SELECT
            1::int AS len
            , idx as idx
            , substring ( 'WTF!' FROM idx FOR 1) as str
            FROM generate_series(1, char_length( 'WTF!' )) idx
            )
            SELECT t0.len as len
                    , t0.idx
                    , t0.str
            FROM no_cte t0
            UNION SELECT 1+t1.len
                    , tc.idx
                    , t1.str || tc.str AS str
            FROM xtab t1
            JOIN no_cte tc ON tc.idx > t1.idx
            )
    SELECT * FROM xtab
    ORDER BY len, str
    -- WHERE len=2
            ;
    

    Results 3:

     len | idx | str  
    -----+-----+------
       1 |   4 | !
       1 |   3 | F
       1 |   2 | T
       1 |   1 | W
       2 |   4 | F!
       2 |   4 | T!
       2 |   3 | TF
       2 |   4 | W!
       2 |   3 | WF
       2 |   2 | WT
       3 |   4 | TF!
       3 |   4 | WF!
       3 |   4 | WT!
       3 |   3 | WTF
       4 |   4 | WTF!
    (15 rows)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to make a function that will return an element of type point:
I want to make a function that automatically turns every Math function global example:
My question for today is: How can I make function that could find all
Three ways I see here: 1) make function return that value explicitly 2) add
i am writing pl/pgsql function that does some statistics processing. using 8.2 of postgres.
I´m trying to make a function that reads elements from an array and distributes
I need to make a Function that receives a Session Key and a Session
I wanted to make a function that populates a Grid in WPF with pictures.
I want to make a function that is always running in xcode similar to
Im trying to make a function that retrieves a value from the db, but

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.