Examine the structure and data in the PRICE_LIST table:
Name . Null . Type
———————-
PROD_ID . NOT NULL . NUMBER(3)
PROD_PRICE . VARCHAR2(10)
PROD_ID PROD_PRICE
———————-
100 $234.55
101 $6, 509.75
102 $1, 234
You plan to give a discount of 25% on the product price and need to display the discount amount in the same
format as the PROD_PRICE.
Which SQL statement would give the required result?
A.
SELECT TO_CHAR(prod_price* .25, ‘$99, 999.99’)
FROM PRICE_LIST;
B.
SELECT TO_CHAR(TO_NUMBER(prod_price)* .25, ‘$99, 999.00’)
FROM PRICE_LIST;
C.
SELECT TO_CHAR(TO_NUMBER(prod_price, ‘$99, 999.99’)* .25, ‘$99, 999.00’) FROM PRICE_LIST;
D.
SELECT TO_NUMBER(TO_NUMBER(prod_price, ‘$99, 999.99’)* .25, ‘$99, 999.00’) FROM PRICE_LIST;
Explanation:
Use TO_NUMBER on the prod_price column to convert from char to number to be able to multiply it with 0.25.
Then use the TO_CHAR function (with formatting’$99, 999.00′) to convert the number back to char.
Incorrect:
Not C: Use the formatting’$99, 999.00′ with the TO_CHAR function, not with the TO_NUMBER function.
Note:
* Using the TO_CHAR Function
The TO_CHAR function returns an item of data type VARCHAR2. When applied to items of type NUMBER,
several formatting options are available. The syntax is as follows:
TO_CHAR(number1, [format], [nls_parameter]),
The number1 parameter is mandatory and must be a value that either is or can be implicitly converted into a
number. The optional format parameter may be used to specify numeric formatting information like width,
currency symbol, the position of a decimal point, and group (or thousands) separators and must be enclosed in
single
* Syntax of Explicit Data Type Conversion
FunctionsTO_NUMBER(char1, [format mask], [nls_parameters]) = num1
TO_CHAR(num1, [format mask], [nls_parameters]) = char1
TO_DATE(char1, [format mask], [nls_parameters]) = date1
TO_CHAR(date1, [format mask], [nls_parameters]) = char1
Answer should be C
B should be wrong. It has not define the number format in TO_NUMBER().
“The TO_NUMBER function converts character items into numbers. If you convert a number using a shorter format mask, an error is returned. If you convert a number based on a longer format mask, the original number is returned. Be careful not to confuse TO_NUMBER conversions with TO_CHAR.
For example, TO_NUMBER(123.56,’999.9′) returns an error, while TO_CHAR(123.56,’999.9′) returns 123.6.”
C is wrong
Yet ‘$99,999.99’ covers all the records on PRICE_LIST, therefore C is right. B is wrong, as edm said.
Thank you!
Wait, I think dem said B is wrong, because it didn’t define the format of the number they want to convert
A: SELECT TO_CHAR(city* .25, ‘$99,999.99’) FROM test;
B: SELECT TO_CHAR(TO_NUMBER(city)*.25, ‘$99,999.00’) FROM test; *******
C: SELECT TO_CHAR(TO_NUMBER(city, ‘$99,999.99’)*.25, ‘$99,999.00’) FROM test;
C: SELECT TO_NUMBER(TO_NUMBER(city, ‘$99,999.99’)* .25, ‘$99,999.00’) FROM test;
B It’s the correct answer.