Which query gives the required result?

Examine the structure of the TRANSACTIONS table:
name Null Type
TRANS_ID NOT NULL NUMBER(3)
CUST_NAME VARCHAR2(30)
TRANS_DATE TIMESTAMPTRANS_AMT NUMBER(10,2)
You want to display the date, time, and transaction amount of transactions that where done before 12 noon.
The value zero should be displayed for transactions where the transaction amount has not been entered.
Which query gives the required result?

Examine the structure of the TRANSACTIONS table:
name Null Type
TRANS_ID NOT NULL NUMBER(3)
CUST_NAME VARCHAR2(30)
TRANS_DATE TIMESTAMPTRANS_AMT NUMBER(10,2)
You want to display the date, time, and transaction amount of transactions that where done before 12 noon.
The value zero should be displayed for transactions where the transaction amount has not been entered.
Which query gives the required result?

A.
SELECT TO_CHAR(trans_date,’dd-mon-yyyy hh24:mi:ss’),
TO_CHAR(trans_amt,’$99999999D99′)
FROM transactions
WHERE TO_NUMBER(TO_DATE(trans_date,’hh24′)) < 12 AND COALESCE(trans_amt,NULL)<>NULL;

B.
SELECT TO_CHAR(trans_date,’dd-mon-yyyy hh24:mi:ss’),
NVL(TO_CHAR(trans_amt,’$99999999D99′),0)
FROM transactions
WHERE TO_CHAR(trans_date,’hh24′) < 12;

C.
SELECT TO_CHAR(trans_date,’dd-mon-yyyy hh24:mi:ss’),
COALESCE(TO_NUMBER(trans_amt,’$99999999.99′),0)
FROM transactions
WHERE TO_DATE(trans_date,’hh24′) < 12;

D.
SELECT TO_DATE (trans_date,’dd-mon-yyyy hh24:mi:ss’),
NVL2(trans_amt,TO_NUMBER(trans_amt,’$99999999.99′), 0)
FROM transactions
WHERE TO_DATE(trans_date,’hh24′) < 12;



Leave a Reply 14

Your email address will not be published. Required fields are marked *


Surendar

Surendar

I think opt B is the wrong answer. Bcoz in the NVL method.. both the comparison term and the actual term should be of same data type

davor

davor

Number can always be converted to string, so it will work.

banu

banu

in a NVL function both arguments should be of the same data type.
Then how does B work ?

Surendar

Surendar

I think again the the character TO_CHAR(trans_amt,’$99999999D99′) will be implicitly converted to number.

Dragana

Dragana

Actualy, 0 will be implicitly converted to character array.

Surendar

Surendar

Characters can be implicity converted to numbers or date. but the reverse is not possible in SQL.

Dragana

Dragana

select last_name||’ ‘||salary
from employees

Surendar

Surendar

What you mean to say?

Dragana

Dragana

In this query we have IMPLICIT conversion of a number to character array, salary (number) is converted to characters and concatenated to last_name (varchar2).
Implicit conversion of numbers and dates to characters is always successful.

Surendar

Surendar

Sorry. Numbers and dates are implicitly converted to character. but the reverse is not implicitly possible in SQL.

Justyna

Justyna

Yes, 0 will be converted. See:
“The arguments expr1 and expr2 can have any datatype. If their datatypes are different, then Oracle Database implicitly converts one to the other. If they are cannot be converted implicitly, the database returns an error. The implicit conversion is implemented as follows:

If expr1 is character data, then Oracle Database converts expr2 to the datatype of expr1 before comparing them and returns VARCHAR2 in the character set of expr1.

If expr1 is numeric, then Oracle determines which argument has the highest numeric precedence, implicitly converts the other argument to that datatype, and returns that datatype. ”

http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions110.htm

Bhavin

Bhavin

i think it should be < '12'….quotes should be present

John

John

Nope, it won’t work this way.