3

say I have a table customer with columns Firstname and lastname I would like to be able to write a query that selects all the customers which has exact lastname and the fuzzy firstname

The Substring is returning the values as expected when I run this just on the firstname column

Query

SELECT SUBSTR(firstname,1,4) from customer;

**output:**
ABCD
qwry
cvbn etc

But I am not sure how to get this work to do a match for 2 records, one of the customers having first 4 characters of firstname same as the other customer firstname.

Example:

Customer1:Firstname=ABCDXYZ Lastname=SSS

Customer2:Firstname=ABCD Lastname=SSS

The expected outcome is to be able to see these 2 records in the output

1
  • what is you question ? Commented Oct 3, 2017 at 17:29

2 Answers 2

1

The below query will fetch the records who has same last names and same first four characters of first name. It uses self join. Hope the table has a primary key like id.

SELECT distinct c1.firstname, 
                c1.lastname, 
                c2.firstname, 
                c1.lastname
  FROM customer c1, customer c2
 WHERE c1.id <> c2.id 
   AND c1.lastname = c2.lastname
   AND substr(c1.firstname,1,4) = substr(c1.firstname,1,4)
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! small correction substr(c1.firstname,1,4) = substr(c2.firstname,1,4)
oh ya thats a typo
0

Not totally sure I understand what you are looking to do but if you know the firstName common "prefix" you can do like this :

SELECT * from Customer WHERE firstname LIKE 'ABCD%' AND lastName = 'SSS';

1 Comment

Basically I am trying to find matches based on fuzzy firstname(2 customers records who has match on first 4 characters of the firstname) and exact lastname.I am planning to extract a potential match report from customer table.Having said that I don't have information of customers who has the same last name and firstname fuzzy.That is what I am trying to achieve by running this query.

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.