-1

I need to write sql query for get similar records from table.It has 100 of records with difference of values. But some records have similar name.Similar names start with capital or simple.

ex- I want to get all the similar records who has similar names.

Student_no | student_Name | Subject   |class
  1        | Abc          | Java      | 1
  2        | Abc          | Java      | 1
  3        | xyz          | AngularJS | 2 
  4        | xyz          | AngularJS | 2
  5        | Abc Def      | SpringBoot| 3

2 Answers 2

1

Use EXISTS and the LIKE operator:

select t.*
from tablename t
where exists (
  select 1 from tablename
  where
    Student_no <> t.Student_no
    and (
      student_Name like concat(t.student_Name, '%')
      or
      t.student_Name like concat(student_Name, '%')
    )
)

See the demo.
Results:

| Student_no | student_Name | Subject    | class |
| ---------- | ------------ | ---------- | ----- |
| 1          | Abc          | Java       | 1     |
| 2          | Abc          | Java       | 1     |
| 3          | xyz          | AngularJS  | 2     |
| 4          | xyz          | AngularJS  | 2     |
| 5          | Abc Def      | SpringBoot | 3     |
Sign up to request clarification or add additional context in comments.

Comments

0

In your instance, a statement like:

SELECT * FROM TABLE WHERE UPPER(student_Name) like UPPER('xyz')

would shift all student_Name values to uppercase, then retrieve all uppercase (or, just all) "XYZ" values.

If not all names are like "xyz" (or, some are "xyz1", "1_xyz", or "ABCXYZDEF"), you need to use like with % representing any number of missing values:

SELECT * FROM TABLE WHERE UPPER(student_Name) like UPPER('%xyz%')

Please see the attached link for more information: SQL- Ignore case while searching for a string

1 Comment

Thank-you @CannonJ95. But I have different names. I can't change the query's like part one by one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.