问题
Below shows a executable statement: Successful attempt:
INSERT INTO Personnel_Assignment (DATE, testno, Hours)
SELECT '21-OCT-2011', '12345',
CASE
WHEN Extract(day From(S.ENDTIME-S.STARTTIME) ) >= 1
THEN (Extract(Day From(S.ENDTIME-S.STARTTIME) ) * 24
+ Extract(Hour From(S.ENDTIME-S.STARTTIME) ) )
WHEN S.endtime IS NULL
THEN NULL
ELSE
Extract(Hour From(S.ENDTIME-S.STARTTIME) ) )
||'hrs' End ||
Extract(Minute From(S.ENDTIME-S.STARTTIME) ) || 'Min' As Hours
FROM Schedule S`
Please note that the data type for endtime and start time is timestamp with timezone in this format:
Nls_Timestamp_Tz_Format='HH24:MI TZR'
Just a question that i would like to ask: My datatype for hours is varchar2 And if i wish to sum my hours in the end from the results above, would it be tedious in converting it into number?
Thanks
回答1:
First of all, || Else
doesn't make sense. The part after ||
has to be another expression to concatenate.
Secondly, you certainly can nest case
expressions, but in your case you don't need to. A single case
expression can have multiple when
/then
branches, in the form case when [condition_A] then [expression_if_A_is_true] when [condition_B] then [expression_if_A_is_false_and_B_is_true] else [expression_if_A_and_B_are_both_false] end
.
来源:https://stackoverflow.com/questions/8176902/using-case-when-to-insert-values