-3

I have table grocery_items with column plu_items which has 12 digit upc code, for ex 123456789123.

How to to remove the last check digit i:e "3" in above ex. in a sql query?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    Possible duplicate of [Strip last two characters of a column in MySQL](https://stackoverflow.com/questions/6080662/strip-last-two-characters-of-a-column-in-mysql) – Ali Sheikhpour Oct 26 '17 at 11:43

2 Answers2

1

Just use left():

select left(plu_items, length(plu_items) - 1)
from grocery_items;

You should review MySQL string functions. They are well-documented.

If you actually want to remove the last digit from the data, use update:

update grocery_items
    set plu_items = left(plu_items, length(plu_items) - 1);

Be careful, because this affects all the data.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You can just divide by 10

select cast(plu_items / 10 as unsigned) as plu_items
from grocery_items 
juergen d
  • 201,996
  • 37
  • 293
  • 362