search

Home  >  Q&A  >  body text

How to get the first name from customer_name column

select substring(custmer_name, 1, instr(custmer_name, ' ')) as first_name from sales.customers;

This solution gave me the answer, but it doesn't work for the last name

P粉908643611P粉908643611241 days ago619

reply all(2)I'll reply

  • P粉884548619

    P粉8845486192024-04-07 16:34:04

    Using SUBSTRING_INDEX()Requires 3 parameters:

    1. Column name
    2. Delimiter
    3. The number of occurrences

    You can find more instructions here Article

    Inquire

    SELECT
      SUBSTRING_INDEX(customer_name,' ', 1) as first_name, 
      SUBSTRING_INDEX(customer_name,' ', -1) as last_name FROM customer;

    reply
    0
  • P粉276064178

    P粉2760641782024-04-07 14:24:41

    Please test it: I use the locate function to define the location of " ".

    SELECT 
    LEFT(customer_name, LOCATE(' ',customer_name)-1) as first_name,
    RIGHT(customer_name, LENGTH(customer_name)-LOCATE(' ',customer_name)) as last_name
    FROM customer;

    Result set:

    reply
    0
  • Cancelreply