How to remove everything after certain character in SQL?

user3446325 picture user3446325 · Mar 21, 2014 · Viewed 8.6k times · Source

I've got a list 400 rows +. Each row looks similar to this: example-example123 I would like to remove everything past '-' so that I'm left with just the beginning part: example123 Any help would be greatly appreciated.

Answer

CodeBird picture CodeBird · Mar 21, 2014

try it like this:

UPDATE table SET column_name=LEFT(column_name, INSTR(column_name, '-')-1) 
WHERE INSTR(column_name, '-')>0;

If you only want to select you do it this way:

SELECT LEFT(column_name, INSTR(column_name, '-')-1) FROM table;

INSTR function gets you the position of your - then you update the column value to become from the first letter of the string till the position of the - -1

Here's a fiddle