Snehal
Snehal

Reputation: 137

Keep first 2 characters in column value and delete rest of the characters

I want to keep first 2 characters of the column value and delete rest characters in mysql table.

+----------------+
| id | firstname |
+----------------+
| 1  | XYZUUIJ   |
| 2  | ABCF      |
+----------------+

Result :

+----------------+
| id | firstname |
+----------------+
| 1  | XY        |
| 2  | AB        |
+----------------+

Upvotes: 2

Views: 186

Answers (3)

flyingfox
flyingfox

Reputation: 13506

You can use substring() to do it

UPDATE TABLE1 SET firstname=SUBSTRING(firstname,1,2); 

Upvotes: 4

saravanatn
saravanatn

Reputation: 630

update table set firstname=substr(firstname,1,2)

Upvotes: 4

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

Use left() :

select id, left(firstname, 2) as firstname
from table t;

I think you don't need to delete or update the table, you can use SELECT statement with LEFT()

Upvotes: 4

Related Questions