Traveling Tech Guy
Traveling Tech Guy

Reputation: 27809

SQL: selecting distinct substring from a field

I'm blacking out on my basic SQL and would appreciate a quick hand:
I have a SQLite table, with 2 columns: Datetime, and a string saying something like "call from 555-555-3344".

I need a simple query that will give me a count of all distinct phone numbers that called on a certain day.

If the field had contained just the number, I could have used Select Distinct on it. How do I do it if the value (phone number) is a substring in that field (though always the last 10 digits).

Assistance, as always, much appreciated.
Guy

Upvotes: 1

Views: 5596

Answers (1)

João Silva
João Silva

Reputation: 91329

You can use the following (I used 12 instead of 10 in order to include the separator -):

SELECT COUNT(DISTINCT SUBSTR(phone_nbr, -12)) 
FROM table
WHERE call_dt = :call_dt;

Upvotes: 1

Related Questions