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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:19:57+00:00 2026-06-07T00:19:57+00:00

I have two columns say Main and Sub . (they can be of same

  • 0

I have two columns say Main and Sub. (they can be of same table or not).

Main is varchar of length 20 and Sub is varchar of length 8.
Sub is always subset of Main and it is last 8 characters of Main.

I could successfully design a query to match pattern using substr("Main",13,8)

Query:

select * from "MainTable"
 where substr("MainColumn",13,8) LIKE (
   select "SubColumn" From "SubTable" Where "SubId"=1043);

but I want to use Like, % , _ etc in my query so that I can loosely match the pattern (that is not all 8 characters).

Question is how can i do that.?!

I know that the query below is COMPLETELY WRONG but I want to achieve something like this,

Select * from "MainTable"
 Where "MainColumn" Like '%' Select "SubColumn" From "SubTable" Where "SubId"=2'
  • 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-07T00:20:00+00:00Added an answer on June 7, 2026 at 12:20 am

    The answers so far fail to address your question:

    but I want use Like, % , _ etc in my query so that I can loosely match
    the pattern (that is not all 8 characters).

    It makes hardly any difference whether you use LIKE or = as long as you match the whole string (and there are no wildcard character in your string). To make the search fuzzy, you need to replace part of the pattern, not just add to it.

    For instance, to match on the last 7 (instead of 8) characters of subcolumn:

    SELECT *
    FROM   maintable m
    WHERE  left(maincolumn, 8) LIKE 
           ( '%' || left((SELECT subcolumn FROM subtable WHERE subid = 2), 7));
    

    I use the simpler left() (introduced with Postgres 9.1).
    You could simplify this to:

    SELECT *
    FROM   maintable m
    WHERE  left(maincolumn, 7) =
           (SELECT left(subcolumn,7) FROM subtable WHERE subid = 2);
    

    But you wouldn’t if you use the special index I mention further down, because expressions in functional indexes have to matched precisely to be of use.

    You may be interested in the extension pg_tgrm.

    In PostgreSQL 9.1 run once per database:

    CREATE EXTENSION pg_tgrm;
    

    Two reasons:

    • It supplies the similarity operator %. With it you can build a smart similarity search:

      --SELECT show_limit();
      SELECT set_limit(0.5); -- adjust similarity limit for % operator
      
      SELECT *
      FROM maintable m
      WHERE left(maincolumn, 8) %
            (SELECT subcolumn FROM subtable WHERE subid = 2);
      
    • It supplies index support for both LIKE and %

      If read performance is more important than write performance, I suggest you create a functional GIN or GiST index like this:

      CREATE INDEX maintable_maincol_tgrm_idx ON maintable
      USING gist (left(maincolumn, 8) gist_trgm_ops);
      

      This index supports either query. Be aware that it comes with some cost for write operations.
      A quick benchmark for a similar case in this related answer.

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

Sidebar

Related Questions

I have two lists ( not java lists, you can say two columns) For
I have two tables with similar columns - let's say table A with column
Lets say I have a table with just two columns: name and mood .
Lets say I have two tables: table english which has two columns, id and
Say I have table with two columns: Name, Age. I want to design a
I have two columns in my table and the rows are created dynamically. <table
I have two columns(column1, column2) in my oracle database table named as demo .
I have two columns in a table that are populated with integer values. For
I have table with two columns (startdate & enddate) and I have two submission
I have a AdvancedDatagrid with two columns lets say. First column has item renderer

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.