1

I am new to SQL however, I am looking to update a table where column called "scrap_value" and rows contains a set of numbers in an array e.g. {100000,125000,150000,175000,200000}

I want to remove the first number and comma of all rows in "scrap_value", so the end result looks like this {125000,150000,175000,200000}

How would I got about doing this?

2
  • 1
    Provide table's DDL (as CREATE TABLE) and a couple of records (as INSERT INTO).
    – Akina
    Commented Jan 16, 2020 at 9:08
  • Is scrap_value a string? Or an array? Commented Jan 17, 2020 at 12:06

2 Answers 2

4

That looks like an array. If that is the case, you can remove the first element of the array using an UPDATE statement:

update the_table
  scrap_value = scrap_value[2:];

[2:] selects all elements of the array starting with the second. The result of that is then used to override the existing array.

3
  • Sorry for the lack of information, I am still learning. Yes, it is in fact an array. I will try out your solution. Thank you. Commented Jan 16, 2020 at 9:26
  • Can confirm. This worked flawlessly for me. Thanks Commented Jan 16, 2020 at 14:03
  • misses definitly the advice to overwork and normalize the data .. its usually a hint to bad design having multiple values in a single cell - array or not
    – eagle275
    Commented Jan 16, 2020 at 16:18
2

column called "scrap_value" ... contains a set of numbers e.g. {100000,125000,150000,175000,200000}

Whilst many DBMSs will support this sort of thing, it breaks basic Database Normalisation rules and, since you say you're just starting out with this sort of thing, let's set you on the Right Road to being with:

These values should be split out into a separate table containing the key of the original table, plus the new value, one per row. This makes it very easy to remove an item, using only a standard Delete statement:

select * from scraps; 
+-----------+--------+
| parent_id | value  | 
+-----------+--------+
|    123456 | 100000 | 
|    123456 | 125000 | 
|    123456 | 150000 | 
|    123456 | 175000 | 
|    123456 | 200000 | 
+-----------+--------+

delete 
from scraps 
where id = 123456 
and value = 175000 ;  

select * from scraps; 
+-----------+--------+
| parent_id | value  | 
+-----------+--------+
|    123456 | 100000 | 
|    123456 | 125000 | 
|    123456 | 150000 | 
|    123456 | 200000 | 
+-----------+--------+

This also makes it far, far easier to search for scrap values.

If you keep to the current "Lots-of-Values-all-Smooshed-Together" model, you have to do all sorts of weird, String manipulation to find an item that might be at the start of the string, or somewhere in the middle or at the end, all with different delimiters before and after the thing you're looking for. Yuck!.

Using the properly normalised structure, you can just use a standard where clause :

select * 
from scraps 
where parent_id = 123456
and value = '150000 ; 

select * 
from scraps; 

+-----------+--------+
| parent_id | value  | 
+-----------+--------+
|    123456 | 150000 | 
+-----------+--------+

To match more than scrap, things get a little more exciting:

select * 
from scraps
where parent_id = 123456 
and value in ( 100000, 200000 ); 

+-----------+--------+
| parent_id | value  | 
+-----------+--------+
|    123456 | 200000 | 
|    123456 | 100000 | 
+-----------+--------+

Regards, Phill W.

1
  • in fact I wanted to assign +10 rating ... Array or not .. its obviously a m:n relation put in a single column / table ... those are to be split into separate tables to have 1:m , 1:n relations
    – eagle275
    Commented Jan 16, 2020 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.