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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:27:23+00:00 2026-06-10T17:27:23+00:00

I want a SQL like query and have results that start with the input

  • 0

I want a SQL like query and have results that start with the input paramater ordered first and the remainder of result will be sorted alphabetically.

So if I have

Foobbb
aaafoo
Fooaaa
bFoob
cccfooccc

and I search for foo I want it sorted as:

Fooaaa
Foobbb
aaafoo
bFoob
cccfooccc

so that me most releivant items are first. Want it a clear to understand and good perofmance way to do this? I could create a temp table but thought it too much overhead.

I tried

DECLARE cemployer CURSOR
WITH RETURN
FOR SELECT employer
    FROM   ((SELECT employer_name,  1 AS grp
             FROM   employer e
             WHERE  Upper(employer_name) LIKE Upper(i_employer|| '%'))
            UNION
            (SELECT employer_name, 2   AS grp
             FROM   employer e
             WHERE  Upper(employer_name) LIKE Upper('%'||i_employer|| '%')))
    ORDER  BY grp,
              employer;

OPEN cemployer;  

However when I do this i get

Fooaaa
Foobbb

repeated again at the bottom of the result set. DB2 will not allow me to put a distinct on the outter query. I know i could solve this problem a number of ways by doing substring or locate or other string functions but wanted to know the most elegant way to do this and ensure no duplictes are returned.

  • 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-10T17:27:24+00:00Added an answer on June 10, 2026 at 5:27 pm

    General method

    ORDER BY CASE WHEN col like 'foo%' THEN 1 ELSE 2 END,col
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a result from an SQL query that I would like to sort
I have a working query that will return some results(records) from my database, like:
I want to make one question about oracle/sql query. I have some data like
I want to use a query like: sql(select name where id > 10 and
I want to write a SQL query like the following. Its syntax is not
I want to write an SQL query using the LIKE keyword. It should search
I have an SQL table like this : sales(product,timestamp) I want to display a
I want to cach input, which seems to be like SQL injection. I know
I have a problem displaying the results of an sql query into a ListView
I have an SQL query that executes a LEFT JOIN on another table, then

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.