QuestionQ2

VerifiedUsing Conversion Functions and Conditional Expressions

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?

Explanation

Because PROD_PRICE is stored as formatted text containing a dollar sign and thousands separator, Oracle cannot implicitly convert it to a number, so TO_NUMBER must be called with an explicit format mask matching that display format to correctly parse the value before the arithmetic is performed; the result must then be passed to TO_CHAR with a currency format mask to render it back as a formatted string matching PROD_PRICE's style, which is exactly what nesting TO_NUMBER(prod_price, format) inside TO_CHAR(..., format) accomplishes.

Community Discussion

No comments yet. Be the first to start the discussion!