Arithmetic and concatenation operators (calculated columns) - SQL

 We learned that operators are symbols that allow you to perform different types of operations.

We said that Oracle has 4 types of operators

  • relational or comparison (we saw them),
  • arithmetic, 
  • concatenation and 
  • logical.

Arithmetic operators allow you to perform calculations with numeric values.


They are: multiplication (*), division (/), addition (+) and subtraction (-).


It is possible to obtain outputs in which a column is the result of a calculation and not a field of a table.


If we want to see the titles, price and quantity of each book we write the following sentence:


 select title, price, quantity

  from books;

If we want to know the total amount in money of a title we can multiply the price by the amount for each title, but we can also have Oracle perform the calculation and include it in an extra column in the output:


 select title, price, quantity,

  price * quantity

  from books;

If we want to know the price of each book with a 10% discount, we can include the following calculations in the sentence:


 select title, price,

  price- (price * 0.1)

  from books;

We can also update the data using arithmetic operators:


 update books set price = price- (price * 0.1);

To concatenate character strings there is the concatenation operator ||.


To concatenate the title and author of each book we use the concatenation operator ("||"):


select title || '-' || author

  from books;

Notice that we also concatenate a dash to separate the fields.


Oracle can automatically convert numeric values ​​to strings for concatenation; For example, in the following example we show the title and price of each book concatenated with the operator "||":


select title || ' $ '|| price

  from books;

Comments

Popular posts from this blog

Data Consistency and Inconsistency - What is Difference

Cardinality in DBMS – max/minimum cardinality & examples

Relational Model in DBMS