Do a "Update" with a sum of a number that is as a character

-1

I need to do a% with_quality with sum in a table whose column is of type update . Example:

UPDATE produto SET codigo = 'codigo'+ 1;

I have the following return:

UPDATE produto SET codigo = 'codigo'+ 1;
ERROR:  invalid input syntax for integer: "codigo"
LINHA 1: UPDATE produto SET codigo = 'codigo'+ 1;

If you are wrong, please tell me the correct method.

    
asked by anonymous 27.07.2018 / 16:06

2 answers

2

It's quite simple, make the column numeric.

Use text for descriptive columns, even if you have only numeric digits in it. And use numeric columns when you need to do accounts like this.

It's even possible to do it this way, but it's so more complicated that the ideal is to change the column. Do not work with misconceptions that the problem is getting bigger.

If you enter, you will need to first convert the column to numeric, make the count by adding 1 and then converting it back to text.

UPDATE produto SET codigo = to_char(to_number(codigo, '999999') + 1);

But this can vary according to your need.

Understand the difference between what is the given, what is the column, the syntax, what does each thing in the code. Without understanding what each thing does in your code will not be able to program. And this is the easy part of programming. You have no idea the hard part. And how easy it is to do everything wrong and work and think you are getting some results, when you are causing huge problems.

    
27.07.2018 / 16:17
0

You can do this:

  UPDATE produto SET codigo = produto.codigo + 1;

But remember that you will be adding + 1 to all your code because you are not passing any conditions to UPDATE.

    
27.07.2018 / 18:31