Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove everything after certain character in SQL?

Tags:

removeall

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.

like image 671
user3446325 Avatar asked Dec 02 '25 08:12

user3446325


1 Answers

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

like image 169
CodeBird Avatar answered Dec 09 '25 01:12

CodeBird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!