Tyssen
Tyssen

Reputation: 1711

MYSQL – how to remove characters from the middle of a string

How would I go about removing the middle characters from a string longer than 11 characters but keeping the first four and last four characters?

So for example WOOLAHRA0427 would become WOOL0427.

I know I can use length to limit the selection to strings longer than 11, and am aware of substr, but that will rely on knowing how long my strings are (they vary) and will remove from the beginning, rather than leaving the beginning intact.

Upvotes: 0

Views: 2763

Answers (2)

Jorge Campos
Jorge Campos

Reputation: 23361

You can use substr. This is intended just as an alternative way of what Gordon Already answered.

As this:

select concat( substr(yourField, 1, 4 ), substr(yourField, -4 ) )

Upvotes: 2

Gordon Linoff
Gordon Linoff

Reputation: 1269593

You can just use left(), right(), and concat():

select concat(left(col, 4), right(col, 4))

Upvotes: 4

Related Questions