Possible to delete everything after a 'space' in MySQL field?

10,438

Solution 1

select substring_index('name sub _blah',' ',1)

Solution 2

Try this:

SUBSTRING_INDEX(str,' ', 1)

Or, if you still need TRIM as well:

SUBSTRING_INDEX(TRIM(str),' ', 1)

See e.g. here for additional documentation.

Share:
10,438
JM4
Author by

JM4

I studied engineering/management of technology but got tossed into the world of web development after working years in a project management capacity. Work with healthcare/insurance companies to develop custom database and reporting solutions along with online enrollment applications.

Updated on July 27, 2022

Comments

  • JM4
    JM4 almost 2 years

    Let's assume I have the following field:

    ' name sub _blah '

    I can get rid of the outside spaces with the TRIM() function but I am actually looking to remove everything after the first space (after the trim).

    so:

    'name sub _blah' would turn into 'name'

    I know this is possible in PHP but I am trying to do on a MySQL only call. Is there a function I do not know about for this?

    Thanks!

  • JM4
    JM4 about 13 years
    thanks for the answer. I appreciate the additional links as well
  • phooji
    phooji about 13 years
    +1 For same answer, but 30 seconds sooner. Well played, nick :)
  • Nicola Cossu
    Nicola Cossu about 13 years
    Hi phooji. + 1 even for you ;)
  • Thomas Williams
    Thomas Williams over 6 years
    This saved my life +1