sqloracle-databasesql-like

string matching for first name and last name characters


My table in SQL has a column with persons name and surname like "Mike Baldwin", "Sara Larson" etc. and need to output all the persons whose name starts with "M" or surnames second last letter is "i". How do I write it in script file?

So I made this script :

SELECT * WHERE CLIENT LIKE 'M%' OR CLIENT LIKE '%i_'

but this script outputs persons whose second last letter of the name is also "i".


Solution

  • As I understand; name and surname are two different columns based on your explanation;

    select *
    from table_name
    where name like'M%' OR surname like '%i_';
    

    Shall work.

    Please replace the actual column names in above query

    ============================================================

    Update : Based on explanation; There a single column client which stores name and surname seperated by a space

    select *
    from table_name
    where SUBSTR(client, 1, 1) = 'M'
       OR SUBSTR(client, INSTR(client, ' ', -1) - 1, 1) = 'i';