I am working the following question for my Oracle class. I have the solution below, that works, but I would like to format the dollar output. When I add the “TO_CHAR” statement, I get an error that “FROM” is not where expected.
Homework assignment question:
Use the Oracle database employees table and CASE expression to decode the department id. Display the department id, last name, salary and a column called “New Salary” whose value is based on the following conditions:
If the department id is 10 then 1.25 * salary
If the department id is 90 then 1.5 * salary
If the department id is 130 then 1.75 * salary
Otherwise, display the old salary.
Working statement WITHOUT the “TO_CHAR”:
SELECT LAST_NAME AS "LAST NAME", DEPARTMENT_ID AS "DEPARTMENT ID", SALARY,
CASE DEPARTMENT_ID
WHEN 10 THEN SALARY * 1.25
WHEN 90 THEN SALARY * 1.50
WHEN 130 THEN SALARY * 1.75
ELSE SALARY END AS "NEW SALARY"
FROM EMPLOYEES;
Statement that errors out:
SELECT LAST_NAME AS "LAST NAME, DEPARTMENT_ID AS "DEPARTMENT ID", SALARY,
CASE DEPARTMENT_ID
WHEN 10 THEN TO_CHAR (SALARY*1.25, '$99,999.99')
WHEN 90 THEN TO_CHAR (SALARY*1.50, '$99,999.99')
WHEN 130 THEN TO_CHAR (SALARY*1.75, '$99,999.99')
ELSE SALARY END AS "NEW SALARY"
FROM EMPLOYEES;
THANKS!
Result after correction below:
LAST NAME DEPARTMENT ID SALARY NEW SALARY
King 90 24000 $36,000.00
Kochhar 90 17000 $25,500.00
De Haan 90 17000 $25,500.00
Craig’s explanation of the problem is correct, but instead of just adding yet another TO_CHAR, I would say wrap the whole CASE expression in a single TO_CHAR: