Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract first word to first space - POSTGRES

Tags:

sql

postgresql

I would like to extract only the first word to the first space.

I used the following query:

select upper(substring(substring(descripcion,28),0,length(substring(descripcion,28))-position(' ' in reverse(substring(descripcion,28)))+1)) from evento where descripcion ~ 'Act. datos:Actualización';

But it gives me back everything, not just the first word before the first space.

How can I get the following result

  1. John
  2. Elena
  3. Maria
  4. Marcus
  5. Mario
  6. Ana
  7. Pedro

etc.

like image 457
Elizama Melo Avatar asked May 23 '26 08:05

Elizama Melo


1 Answers

Use split_part with space as delimiter to get the first string, e.g.

SELECT split_part('CARMEN SANDIEGO',' ',1);

 split_part 
------------
 CARMEN

So in your case it should be something like

SELECT 
  upper(
   split_part(
    trim(substring(descripcion,28)),' ',1))
FROM evento
WHERE descripcion ~ 'Act. datos:Actualización';

Demo: db<>fiddle

like image 57
Jim Jones Avatar answered May 25 '26 09:05

Jim Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!