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

  • Home
  • SEARCH
  • 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 6556017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:53:26+00:00 2026-05-25T12:53:26+00:00

Running PostgreSQL 7.x (Yeah I’m upgrading) Example: SELECT CASE WHEN substring(t.Field::text, 0, 3) =

  • 0

Running PostgreSQL 7.x (Yeah I’m upgrading)

Example:

SELECT
    CASE 
        WHEN "substring"(t."Field"::text, 0, 3) = '01'::text THEN 'group one'::text
        WHEN "substring"(t."Field"::text, 0, 4) = '123'::text THEN 'group one'::text
        WHEN "substring"(t."Field"::text, 0, 5) = '4567'::text THEN 'group two'::text
        WHEN "substring"(t."Field"::text, 0, 6) = '99999'::text THEN 'group three'::text
        WHEN "substring"(t."Field"::text, 0, 3) = '07'::text THEN 'group three'::text
        ELSE NULL::text
    END AS new_field,
    CASE 
        WHEN "substring"(t."Field"::text, 0, 3) = '01'::text THEN 'subgroup a'::text
        WHEN "substring"(t."Field"::text, 0, 4) = '123'::text THEN 'subgroup a'::text
        WHEN "substring"(t."Field"::text, 0, 5) = '4567'::text THEN 'subgroup a'::text
        WHEN "substring"(t."Field"::text, 0, 6) = '99999'::text THEN 'subgroup a'::text
        WHEN "substring"(t."Field"::text, 0, 3) = '07'::text THEN 'subgroup b'::text
        ELSE NULL::text
    END AS another_new_field,...

Is there a way to have one case statement give two fields as a result as the data processed is the same it’s just the label that’s different.

  • 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-05-25T12:53:27+00:00Added an answer on May 25, 2026 at 12:53 pm

    I’m not familiar with Postgre, but perhaps You can try a common table expression to build a table indexing your lookup conditions and the output results for group and subgroup, which might look something like this (of course the conditions could come from an actual table as well…):

    with Lookup as (
        select 0 as start, 3 as end,
            '01' as match, 'group one' as sgroup, 'subgroup a' as subgroup union
        select 0 as start, 4 as end,
            '123' as match, 'group one' as sgroup, 'subgroup a' as subgroup union
        select 0 as start, 5 as end,
            '4567' as match, 'group two' as sgroup, 'subgroup a' as subgroup union
        select 0 as start, 6 as end,
            '99999' as match, 'group three' as sgroup, 'subgroup a' as subgroup union
        select 0 as start, 3 as end,
            '07' as match, 'group three' as sgroup, 'subgroup b' as subgroup union
    )
    select
        L.sgroup as new_field,
        L.subgroup as another_new_field
    from table T
    left join Lookup L on "substring"(T."Field"::text, L.start, L.End) = L.match
    

    Then you only have to define the conditions once, and match them once to get both fields outputted. As expressed by @Tom in comment, this kind of scenario is not well handled in SQL, so you’re left with a more “hacky” solution like the above.

    Best Regards,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Running PostgreSQL 7.4(Yep we're upgrading) Field is data type: timestamp with time zone Format
Hi Im running example using hibernate with postgresql 8.4, when i try to add
I'm running PostgreSQL version 9.0 on OSX version 10.6.6. Somehow one of my development
I'm running: PostgreSQL 9.0.3 (Last Debian stable) This query: SELECT * FROM sis.thread_categories stc,
I have a really big database (running on PostgreSQL) containing a lot of tables
I have a Rails 2.0.2 application running with a postgresql db. The machine will
I'm running a query against a table in a postgresql database. The database is
Is there a way to disable the Postgresql translation of messages? I´m running my
I currently have the query running on Postgres: SELECT * FROM addenda.users WHERE users.username
Background : Running a PostgreSQL database for a Django app (Django 1.1.1, Python2.4, psycopg2

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.