Quantcast
Channel: Oracle
Viewing all articles
Browse latest Browse all 1814

Wiki Page: USING TO_CHAR(...'DAY') FUNCTION IN CONDITIONAL LOGIC

$
0
0
USING TO_CHAR(...'DAY') FUNCTION IN CONDITIONAL LOGIC Written JP Vijaykumar Oracle DBA Date Sept 14th 2016 I did not get any error, but the conditional logic is not working as expected. set serverout on size 1000000 timing on declare v_dt date:=to_date('05-14-2016','mm-dd-yyyy'); begin for i in 1..16 loop if ((to_char(v_dt+i,'DAY') != 'SATURDAY') and (to_char(v_dt+i,'DAY') != 'SUNDAY')) then dbms_output.put_line(v_dt+i||' '||to_char(v_dt+i,'DAY')); end if; end loop; end; / 15-MAY-16 SUNDAY 16-MAY-16 MONDAY 17-MAY-16 TUESDAY 18-MAY-16 WEDNESDAY 19-MAY-16 THURSDAY 20-MAY-16 FRIDAY 21-MAY-16 SATURDAY 22-MAY-16 SUNDAY 23-MAY-16 MONDAY 24-MAY-16 TUESDAY 25-MAY-16 WEDNESDAY 26-MAY-16 THURSDAY 27-MAY-16 FRIDAY 28-MAY-16 SATURDAY 29-MAY-16 SUNDAY 30-MAY-16 MONDAY PL/SQL procedure successfully completed. Elapsed: 00:00:00.01 I ran the following test. SQL> set serverout on size 1000000 timing on SQL> declare 2 v_dt date:=to_date('09-14-2016','mm-dd-yyyy'); 3 begin 4 dbms_output.put_line(v_dt||' '||'Today is: '||to_char(v_dt,'DAY')); 5 for i in 1..7 loop 6 if (to_char(v_dt+i,'DAY') = 'SUNDAY') then 7 dbms_output.put_line('Today is: '||to_char(v_dt+i,'DAY')); 8 elsif (to_char(v_dt+i,'DAY') = 'MONDAY') then 9 dbms_output.put_line('Today is: '||to_char(v_dt+i,'DAY')); 10 elsif (to_char(v_dt+i,'DAY') = 'TUESDAY') then 11 dbms_output.put_line('Today is: '||to_char(v_dt+i,'DAY')); 12 elsif (to_char(v_dt+i,'DAY') = 'WEDNESDAY') then 13 dbms_output.put_line('Today is: '||to_char(v_dt+i,'DAY')); 14 elsif (to_char(v_dt+i,'DAY') = 'THURSDAY') then 15 dbms_output.put_line('Today is: '||to_char(v_dt+i,'DAY')); 16 elsif (to_char(v_dt+i,'DAY') = 'FRIDAY') then 17 dbms_output.put_line('Today is: '||to_char(v_dt+i,'DAY')); 18 elsif (to_char(v_dt+i,'DAY') = 'SATURDAY') then 19 dbms_output.put_line('Today is: '||to_char(v_dt+i,'DAY')); 20 end if; 21 --dbms_output.put_line('--------- '||v_dt+i||' '||to_char(v_dt+i,'DAY')||' '||length(to_char(v_dt+i,'DAY'))); --FAILED WITH "ORA-06502" 22 dbms_output.put_line('--------- '||(v_dt+i)||' '||to_char(v_dt+i,'DAY')||' '||length(to_char(v_dt+i,'DAY'))); 23 end loop; 24 end; 25 / 14-SEP-16 Today is: WEDNESDAY --------- 15-SEP-16 THURSDAY 9 --------- 16-SEP-16 FRIDAY 9 --------- 17-SEP-16 SATURDAY 9 --------- 18-SEP-16 SUNDAY 9 --------- 19-SEP-16 MONDAY 9 --------- 20-SEP-16 TUESDAY 9 Today is: WEDNESDAY --------- 21-SEP-16 WEDNESDAY 9 PL/SQL procedure successfully completed. Elapsed: 00:00:00.02 Modified the code, the logic is working as expected. SQL> set serverout on size 1000000 timing on SQL> declare 2 v_dt date:=to_date('09-14-2016','mm-dd-yyyy'); 3 begin 4 dbms_output.put_line(v_dt||' '||'Today is: '||to_char(v_dt,'DAY')); 5 for i in 1..7 loop 6 if ((trim(to_char(v_dt+i,'DAY')) != 'SATURDAY') and (trim(to_char(v_dt+i,'DAY')) != 'SUNDAY')) then --BOTH CONSTRUCTS ARE WORKING 7 --if ((to_char(v_dt+i,'DAY') NOT LIKE '%SATUR%') and (to_char(v_dt+i,'DAY') NOT LIKE '%SUN%')) then --BOTH CONSTRUCTS ARE WORKING 8 --for j in 1..4 loop 9 dbms_output.put_line(v_dt+i||' '||to_char(v_dt+i,'DAY')); 10 --end loop; 11 end if; 12 end loop; 13 end; 14 / 14-SEP-16 Today is: WEDNESDAY 15-SEP-16 THURSDAY 16-SEP-16 FRIDAY 19-SEP-16 MONDAY 20-SEP-16 TUESDAY 21-SEP-16 WEDNESDAY PL/SQL procedure successfully completed. Elapsed: 00:00:00.01 I leave the explanation to the readers' imagination. Happy scripting.

Viewing all articles
Browse latest Browse all 1814

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>