Serge
Serge

Reputation: 741

MySQL how to extract data from one column using Substring and put to another column under same ID within same table

Let's say we have the table PEOPLE like

id      |   name        |       extracted
-----------------------------------------
1       |   Roger       |
-----------------------------------------
2       |   Anthony     |
-----------------------------------------
3       |   Maria       |
-----------------------------------------

We use

SELECT SUBSTRING(name, 1, 3) FROM people.name WHERE name like '%thon%' 

It will find "Anthony" and extract 3 first chars - so result is : Ant

How to put this result against same id so the table looks like

id      |   name        |       extracted
-----------------------------------------
1       |   Roger       |
-----------------------------------------
2       |   Anthony     |       Ant
-----------------------------------------
3       |   Maria       |
-----------------------------------------

Upvotes: 0

Views: 1499

Answers (1)

bansi
bansi

Reputation: 57042

Try

UPDATE people SET extracted = LEFT(`name`,3) WHERE `name` like '%thon%'

Upvotes: 2

Related Questions