Q

Doing SQL arithmetic with SELECT instead of arithmetic operations

Learn how to do simple SQL arithmetic operations without using the arithmetic operators.

I have a small question about SQL arithmetic to ask. I have an employee table that includes two columns: employee...

number and salary. Example:

EMP      SALARY
1            10
2            20
3            30

I have to craft a query to print all rows of the employee table after multiplying the numbers in the salary column by 100. But I can only use a SELECT statement, not any SQL arithmetic operations. So the output should be:

EMP      SALARY
1            1000
2            2000
3            3000

More on SQL arithmetic and other SQL questions

Read more expert answers from Karen Morton

Ask your own expert question

First, I just have to say this question makes me wonder about its purpose. I can't imagine why SQL arithmetic operations couldn't be used in a real-life situation. Anyway, I can easily answer the question, given a couple of assumptions:

  1. Salaries are always stored in the table as multiples of 100.
  2. Salaries are always numeric or null; or if they are non-numeric, the final display answer still makes sense ending in 00.

The answer is:

SELECT emp, to_char(salary) || '00' as salary

FROM employee;

Since this is a concatenation operation, not an arithmetic operation, the answer meets your limiting criteria and provides the answer you want.

This was last published in February 2013

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Join the conversation

7 comments

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

I think this technique is dangerous in long run. Generally developers don't remember their code for long time and that makes the difference. This little piece of code can't be used as a sub-query where the mathematics does not end there. If we remember the rule 'GARBAGE-IN and GARBAGE-OUT', this query would produce wrong result when the salary has NULL or decimal values. So we need to be very careful while using this technique. Well this is only my opinion and not a part of any print.

-Hara
Cancel
I think this technique is dangerous in long run. Generally developers don't remember their code for long time and that makes the difference. This little piece of code can't be used as a sub-query where the mathematics does not end there. If we remember the rule 'GARBAGE-IN and GARBAGE-OUT', this query would produce wrong result when the salary has NULL or decimal values. So we need to be very careful while using this technique. Well this is only my opinion and not a part of any print.

-Hara
Cancel
Just think about
1) resulting datetype
2) nls parameters
Cancel
"First, I just have to say this question makes me wonder about its purpose. I can't imagine why SQL arithmetic operations couldn't be used in a real-life situation."

Why waste your time and your readers time answering useless questions?
Cancel
It is amazing how consistent is Karen in getting simple sings wrong. First, SQL arithmethic (well, actually, math, since there is a full set of functions available) is quite a routine and very useful application of SQL. Second, for many reasons (but you need to know what NULL is to discuss them) the answer is "*100", not "||'00'".
Cancel
How can you writing was te your readers time with crap like this??? Well now I know wich writers shouldn't read anymore... Such a waste Of time...
Cancel
Ridiculous, if its a numeric field.

Why don't you multiply it by 100 for the column.
Cancel

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close