Re: Use TRUNC function to get weekday

From: Mark D Powell <Mark.Powell_at_eds.com>
Date: Wed, 22 Apr 2009 05:53:14 -0700 (PDT)
Message-ID: <72d12b35-f57d-4649-8ff1-2c97128bcfa9_at_n4g2000vba.googlegroups.com>



On Apr 21, 5:26 pm, nickli2..._at_gmail.com wrote:
> Hi,
>
>   Is it possible to use TRUNC function to get the weekday (e.g.
> Monday, Tuesday ...)? I know I can use TO_CHAR to get it:
>
>   select TO_CHAR(trunc(sysdate), 'DAY') weekday, TO_CHAR(sysdate, 'D')
> day_of_week from   dual;
>
>   Thanks in advance.
>
>   Nick Li

I would say the proper way to ge the day of the week from a date data type is to use to_char otherwise the default value behavior for the componenets might be an issue since the result of a trunc is still a date data type.

  1* select to_char(trunc(sysdate,'YYYY'),'YYYYMMDD HH24:MI:SS') from dual
UT1 > /

TO_CHAR(TRUNC(SYS



20090101 00:00:00

UT1 > c /YYYY/MM/
  1* select to_char(trunc(sysdate,'MM'),'YYYYMMDD HH24:MI:SS') from dual
UT1 > /

TO_CHAR(TRUNC(SYS



20090401 00:00:00

UT1 > c /MM/DD/
  1* select to_char(trunc(sysdate,'DD'),'YYYYMMDD HH24:MI:SS') from dual
UT1 > /

TO_CHAR(TRUNC(SYS



20090422 00:00:00

UT1 > l
  1 declare
  2 v_day varchar2(12);
  3 begin
  4 select trunc(sysdate,'DAY') into v_day from dual;   5 dbms_output.put_line(v_day);
  6* end;
UT1 > /
19-APR-09 <= notice date change from today the 22nd to first day of this week

PL/SQL procedure successfully completed.

UT1 > select to_char(trunc(sysdate),'DAY') from dual; -- no trunc necessary, just use to_char

TO_CHAR(T



WEDNESDAY I suggest using to_char to get and work with components of a date data type.

HTH -- Mark D Powell -- Received on Wed Apr 22 2009 - 07:53:14 CDT

Original text of this message