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;
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
Number can always be converted to string, so it will work.
About B, it works:
select nvl(to_char(comm,’999,90′),0) from emp;
NVL(TO_CHAR(COMM,’999,90′),0)
—————————–
0
3,00
5,00
0
14,00
0
0
0
0
0
0
0
0
0
14 rows selected
http://docs.oracle.com/cd/B14117_01/server.101/b10759/sql_elements002.htm
Implicit Data Conversion
in a NVL function both arguments should be of the same data type.
Then how does B work ?
I think again the the character TO_CHAR(trans_amt,’$99999999D99′) will be implicitly converted to number.
Actualy, 0 will be implicitly converted to character array.
Characters can be implicity converted to numbers or date. but the reverse is not possible in SQL.
select last_name||’ ‘||salary
from employees
What you mean to say?
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.
Sorry. Numbers and dates are implicitly converted to character. but the reverse is not implicitly possible in SQL.
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
i think it should be < '12'….quotes should be present
Nope, it won’t work this way.