How to find first and last date of a fiscal week using SQL

Hello,
I want information about FISCAL Week, means a Week based on ISO standard. I know format strings ‘IW’ or ‘IYYY’ gives fiscal week and fiscal year respectively from a given date. But I want to find the first and last date of a fiscal week. Say suppose I have a fiscal week is 2, and fiscal year is 2008, how to find the start and end date of the given fiscal week.
Any kind of help would be greatly appreciable.
Thanks,
Prince

davide gislon wrote:
The following query evaluate the begin of a fisical week, where &year and &week are respectively the year and week you want to calculate.
To evaluate the end of the week you have to add 6.
Note that my database is set to have monday as day number 1 of the week, and sunday as day number 7; if your database settings are different you should modify the query accordingly.
SELECT CASE TO_CHAR(TO_DATE('&year','YYYY'),'D')
WHEN '1' THEN TO_DATE('&year','YYYY')+((&week-1)*7)
WHEN '2' THEN TO_DATE('&year','YYYY')+((&week-1)*7-1)
WHEN '3' THEN TO_DATE('&year','YYYY')+((&week-1)*7-2)
WHEN '4' THEN TO_DATE('&year','YYYY')+((&week-1)*7-3)
WHEN '5' THEN TO_DATE('&year','YYYY')+((&week-1)*7+3)
WHEN '6' THEN TO_DATE('&year','YYYY')+((&week-1)*7+2)
WHEN '7' THEN TO_DATE('&year','YYYY')+((&week-1)*7+1)
END BEGIN_FISICAL_WEEK
FROM DUAL
Hope this is helpful.
Cheers,
Davide
Edited by: davide gislon on 08-Jan-2009 07:19Your query does nothing you say it does. TO_DATE('&year','YYYY') returns first day of the current month for year &year. And the only reason it returns January 1, &year is that we are currently in January:
SQL> select TO_DATE('&year','YYYY') from dual
  2  /
Enter value for year: 2005
old   1: select TO_DATE('&year','YYYY') from dual
new   1: select TO_DATE('2005','YYYY') from dual
TO_DATE('
01-JAN-05
SQL> As soon as we roll into February:
SQL> alter system set fixed_date = '2009-2-1' scope=memory
  2  /
System altered.
SQL> select sysdate from dual
  2  /
SYSDATE
01-FEB-09
SQL> select TO_DATE('&year','YYYY') from dual
  2  /
Enter value for year: 2005
old   1: select TO_DATE('&year','YYYY') from dual
new   1: select TO_DATE('2005','YYYY') from dual
TO_DATE('
01-FEB-05
SQL> alter system set fixed_date = NONE scope=both
  2  /
System altered.
SQL> select sysdate from dual
  2  /
SYSDATE
08-JAN-09
SQL> But even if TO_DATE('&year','YYYY') would always return January 1, &year, or you would fix it to TO_DATE('0101&year','MMDDYYYY') it still would be wrong. ISO week rules are
If January 1 falls on a Friday, Saturday, or Sunday, then the week including January 1 is the last week of the previous year, because most of the days in the week belong to the previous year.
If January 1 falls on a Monday, Tuesday, Wednesday, or Thursday, then the week is the first week of the new year, because most of the days in the week belong to the new year.Therefore, next year:
SQL> DEFINE YEAR=2010
SQL> DEFINE WEEK=1
SQL> ALTER SESSION SET NLS_TERRITORY=GERMANY -- enforce Monday as first day of the week
  2  /
Session altered.
SQL> SET VERIFY OFF
SQL> SELECT CASE TO_CHAR(TO_DATE('0101&&year','MMDDYYYY'),'D')
  2  WHEN '1' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7)
  3  WHEN '2' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7-1)
  4  WHEN '3' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7-2)
  5  WHEN '4' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7-3)
  6  WHEN '5' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7+3)
  7  WHEN '6' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7+2)
  8  WHEN '7' THEN TO_DATE('0101&&year','MMDDYYYY')+((&&week-1)*7+1)
  9  END BEGIN_FISICAL_WEEK
10  FROM DUAL
11  /
BEGIN_FI
04.01.10
SQL> SELECT TRUNC(TO_DATE('0101&&year','MMDDYYYY'),'IW') FROM DUAL
  2  /
TRUNC(TO
28.12.09
SQL> 2 user10772980:
Use:
SELECT  TRUNC(TO_DATE('0101&&year','MMDDYYYY'),'IW') + (&&week-1)*7 FISCAL_YEAR_&&YEAR._WEEK_&&WEEK
  FROM  DUAL
FISCAL_YEAR_2010_WEEK_1
28.12.09
SQL> SY.

Similar Messages

  • How to get first and last date of previous month

    To set default values in selection screen,I want first and last date of the previous month.
    Also I want the first and last dates of previous quarter.
    The quarters can be only january to march, apr to june, jul to sept, oct to dec.
    I want to run a program on beginning of each month by job scheduling having <b>selection screens</b> for the above monthly and quarterly dates.

    Hi Suhas,
    Use the below code.
    DATA: v_date LIKE sy-datum.
    DATA: v_month_begin_date TYPE sy-datum,
          v_month_end_date TYPE sy-datum,
          v_month(2) TYPE n,
          v_month1(2) TYPE n,
          v_quarter TYPE i,
          v_year(4) TYPE n,
          v_quarter_begda TYPE sy-datum,
          v_quarter_endda TYPE sy-datum.
    v_month =  sy-datum+4(2).
    IF v_month = '01'.
      v_month = '12'.
      v_year = sy-datum+0(4) - 1.
    ELSE.
      v_month = v_month - 1.
      v_year = sy-datum+0(4).
    ENDIF.
    CONCATENATE v_year v_month  '01' INTO v_date.
    CALL FUNCTION 'HR_JP_MONTH_BEGIN_END_DATE'
      EXPORTING
        iv_date             = v_date
      IMPORTING
        ev_month_begin_date = v_month_begin_date
        ev_month_end_date   = v_month_end_date.
    v_month1 = sy-datum+4(2).
    IF v_month1 = '01' OR
       v_month1 = '02' OR
       v_month1 = '03'.
      v_quarter = 1.
    ELSEIF v_month1 = '04' OR
           v_month1 = '05' OR
           v_month1 = '06'.
      v_quarter = 2.
    ELSEIF v_month1 = '07' OR
           v_month1 = '08' OR
           v_month1 = '09'.
      v_quarter = 3.
    ELSEIF v_month1 = '10' OR
           v_month1 = '11' OR
           v_month1 = '12'.
      v_quarter = 4.
    ENDIF.
    IF v_quarter = 1.
      v_quarter = 4.
      v_year = sy-datum+0(4) - 1.
    ELSE.
      v_quarter = v_quarter - 1.
      v_year = sy-datum+0(4).
    ENDIF.
    CALL FUNCTION 'HR_99S_GET_DATES_QUARTER'
      EXPORTING
        im_quarter = v_quarter
        im_year    = v_year
      IMPORTING
        ex_begda   = v_quarter_begda
        ex_endda   = v_quarter_endda.
    WRITE:/5 'previous month begin date : ', v_month_begin_date.
    WRITE:/5 'previous month end date   : ', v_month_end_date.
    WRITE:/5 'previous quarter begin date : ', v_quarter_begda.
    WRITE:/5 'previous quarter end date : ', v_quarter_endda.
    Message was edited by:
            Velangini Showry Maria Kumar Bandanadham

  • How to find first and last day of last month?.

    Hello,
    I am using Crystal Report XI.
    Reports will be generated on first of every month for
    previous month. I want to find start and end date of previous month
    dynamic.
    For august, Start date is July 1st and End date July 31st.
    For September, start date is Aug 1st and Aug 31st.
    How can i get first of previous month as start date and last
    day of the previous month as end date?.
    Same kind of thing I want to do for future report ..find next
    month start date and end date.
    Thanks

    Adomacro,
    you could do a function, like this
    <cffunction name="getReportdates" returntype="struct"
    hint="Given an arbitrary month and year, the function returns a
    structure containing the first and last day of the previous
    month">
    <!--- Take current month and current year as the default
    --->
    <cfargument name="mnth" type="string" required="No"
    default="#monthAsString(month(now()))#">
    <cfargument name="yr" type="numeric" required="No"
    default="#year(now())#">
    <cfset firstDayOfGivenMonth = parseDateTime("1 " &
    arguments.mnth & " #arguments.yr#")>
    <cfset lastDayOfPreviousMonth =
    dateAdd("d",-1,firstDayOfGivenMonth)>
    <cfset numberOfDaysOfPreviousMonth =
    daysInMonth(lastDayOfPreviousMonth)>
    <cfset firstDayOfPreviousMonth =
    dateAdd("d",-#numberOfDaysOfPreviousMonth#,firstDayOfGivenMonth)>
    <cfset reportDates = structNew()>
    <cfset reportDates.firstDayOfPreviousMonth =
    firstDayOfPreviousMonth>
    <cfset reportDates.lastDayOfPreviousMonth =
    lastDayOfPreviousMonth>
    <cfreturn reportDates>
    </cffunction>
    <!--- Example usage --->
    <p>
    Given month: <strong>March
    2004</strong><br>
    begin date:
    <cfoutput>#dateFormat(getReportdates('March',2004).firstDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    end date:
    <cfoutput>#dateFormat(getReportdates('March',2004).lastDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    </p>
    <p>
    Given month: <strong>August
    2007</strong><br>
    begin date:
    <cfoutput>#dateFormat(getReportdates('August',2007).firstDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    end date:
    <cfoutput>#dateFormat(getReportdates('August',2007).lastDayOfPreviousMonth,"d
    mmm yyyy")#</cfoutput><br>
    </p>

  • How to find first and last day of previous month?.

    Based on current month, I want to find start and end day of
    previous month.
    For example,
    For august, Start date is July 1st and End date July 31st.
    How can i get first of previous month as start date and last
    day of the previous month as end date?.
    Same way,
    i want to find start date of current month and end date of
    next month.
    Example:
    For august,
    i want to get start date, august 1st and end date : september
    30.
    How can i do this from current date or now().
    I am looking for best and easy way to find start and end
    dates..
    Thanks

    <cfset today = now()>
    <cfset firstOfThisMonth = createDate(year(today),
    month(today), 1)>
    <cfset lastOfNextMonth = dateAdd("d", -1, dateAdd("m", 2,
    firstOfThisMonth))>
    <cfoutput>
    today = #today#<br>
    firstOfThisMonth = #firstOfThisMonth#<br>
    lastOfNextMonth = #lastOfNextMonth#<br>
    </cfoutput>
    Edit - To find the start and end day of previous month, get
    the first of THIS month. Use Subtract 1 month ("m") to get the
    start date. Subtract 1 day ("d") to get the end date.

  • Query to find first and last call made by selected number for date range

    Hi,
    query to find first and last call made by selected number for date range
    according to filter:
    mobile_no : 989.....
    call_date_from : 25-april-2013
    call_date_to : 26-april-2013
    Please help

    Hi,
    It sounds like you want a Top-N Query , something like this:
    WITH    got_nums   AS
         SELECT     table_x.*     -- or list columns wanted
         ,     ROW_NUMBER () OVER (ORDER BY  call_date      ) AS a_num
         ,     ROW_NUMBER () OVER (ORDER BY  call_date  DESC) AS d_num
         FROM     table_x
         WHERE     mobile_no     = 989
         AND     call_date     >= DATE '2013-04-25'
         AND     call_date     <  DATE '2013-04-26' + 1
    SELECT  *     -- or list all columns except a_num and d_num
    FROM     got_nums
    WHERE     1     IN (a_num, d_num)
    ;This forum is devoted to the SQL*Plus and iSQL*Plus front ends. This question doesn't have anything to do with any front end, does it? In the future, you'll get better response if you post questions like this in the PL/SQL.
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the SQL forum FAQ {message:id=9360002}

  • How to find when and what date my iphone was first switched on from a retailer

    how to find when and what date my iphone was first switched on from a retailer

    Depending on the Code version you are running you can look for this output . It will show when a SFP was removed and inserted.
    module-2# show port-config internal all  (This command wil differ of earlier versions of SAN OS )
    Mar 20 08:53:45 2011  00449361  fc2/20         ---   DOWN   Xcvr inserted          
    Mar 20 08:53:32 2011  00829429  fc2/20         ---   DOWN   Disable                
    Mar 20 08:53:32 2011  00826403  fc2/20         ---   DOWN   Disable                
    Mar 20 08:53:32 2011  00818936  fc2/20         ---   DOWN   Xcvr removed  

  • First and Last Date.  Is is possible in Essbase?

    Hello All,<BR><BR>Here are the dimensions in my outline:<BR>- User<BR>- Application<BR>- Dates<BR>- Measures (Count, FirstLoginDate, LastLoginDate)<BR><BR>I am trying to create a report in Essbase that shows the first and last date that each user accessed each Essbase application. I created an ETL process that builds a relational table with one record for each unique combination of User/Application/Day where a user was logged into an application. This process gets its source data from the Essbase application log files. I am loading a count of 1 to each intersection of User/Application/Day. I also want to add the first and last day to the cube but can not figure out how.<BR><BR>1) Is there an easier way to get at the source data then the application log files?<BR>2) If the application log files are the way to go, how do I indicate in the cube what the first and last login days are for each combination of User/Application?<BR><BR>I was thinking of loading the serial dates to the FirstLoginDate and LastLoginDate measures. The trouble I am having is where to load the dates data. Do I load it to level 0 (individual dates) and then use time balance to get the dates up to the higher levels (month, year) or load it at the higher levels? If I should load it at the higher levels, do I assume correctly I will need to create another relational table with one record for each User/Application/Month and User/Application/Year that shows the first and last dates? Any other ideas?<BR><BR>Thank you in advance,<BR><BR>Bill Handelman<BR>847-989-1758<BR>[email protected]<BR><BR>

    While you may have natural ordering in your date dimensions, Essbase doesn't handle first and last dates well. The one area where ther is at least some funcionality in date manipulation is inattribute dimensions, however, the easy use there allows only one date attribute using normal date processing. It gets a great deal more complex using two dates.<BR><BR>Look up the DBAG references to the date type of attribute dimensions, you might find it a partial solution to your problem.<BR><BR>If you go the serial date method, load at level 0 and want to use time balance to bring the values up, you still need a time dimension. If I were designing something, I might use the first access date as a time dimension and the last access date in an attribute dimension, but I'm rambling rather than analyzing.<BR><BR>In any case, look at the date type attribute dimension as one possible option for your cube.

  • Getting first and last dates of a month

    Jello all,
    I am really sorry I am posting this topic here, not actually owned by this forum but in fundamental forum i am not getting it attended.
    I am generating monthly, quartelry and yearly reports, for which I need to get first and last dates of a given Month taking in to account leap year. I searched the API and docs for Calendar class. I could not find any useful methods / fields. I wonder this being a very common requirement, how SUN has not provided these essential features. I still feel, it must be some where. Please help me and reply. Also if you came across any other forum for util package, let me know.
    Thanks
    Varde

    Jello all,
    I am really sorry I am posting this topic here, not
    actually owned by this forum but in fundamental forum
    i am not getting it attended.
    Yes, and I can see you are a very patient person, waiting a whole half hour for a response before cross posting other boards.
    Question answered.
    http://forum.java.sun.com/thread.jsp?thread=514908&forum=31&message=2451646

  • Get first and last date of month

    Hi,
      Is there any function module to get first and last date of month for a entered date.
    Please let me know.
    Regards,
    SP

    Hi,
    Use the below FM to find the Last day of month and them u can easily calculate the first day.
    DATA : v_startdate TYPE sy-datum.
    DATA : v_enddate TYPE sy-datum.
    DATA : v_temp TYPE dats.
    v_temp = sy-datum.
    CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
        EXPORTING
          day_in            = v_temp
        IMPORTING
          last_day_of_month = v_enddate
    CONCATENATE  v_enddate+0(6)  '01'  INTO  v_startdate.
    Hope it helps.
    Regards,
    Arnab.

  • How to find out the last date & time on which a program has been run

    Hi Experts,
    Can anybody tell how to find out the last date on which a purticular program has been run.
    Valuable answers will be rewarded.
    Thanks & Regards,
    Satish.

    Hi!
    Try STAD transaction. Unfortunately it contains a huge amount of data, so it is available only for a few days backwards... Maybe some weeks, but you can't make yearly reports from its data... So it's not useful to see, which program is in use or not...
    I planned once to download it weekly, but it was not so important...
    Regards
    Tamá

  • Finding first and last members of a group set

    Is there any example how to use 'first' and 'last' functions in an sql query ?
    I have tried to execute a query like this on the scott.emp table :
    select deptno,min(sal),max(sal),first(sal) from emp group by deptno;
    but I always get this message:
    ERROR at line 1:
    ORA-00904: "FIRST": invalid identifier
    btw I use Oracle RDBMS ver 9.1.0.3 for Solaris in my server.
    tx for your help
    sjarif

    Syarif,
    The FIRST and LAST functions, which became available with 9i, are used to find the first or last member of a ranked set. I'm not sure exactly what you are trying to do, but I can show you a query that should work (I don't have 9i handy at the moment). This query should give you the departments with the highest (first) and lowest (last) aggregate salaries:
    select deptno,
    min(deptno)
    keep (dense_rank first order by sum(sal) desc) highest_sal,
    min(deptno)
    keep (dense_rank last order by sum(sal) desc) lowest_sal
    from emp
    group by deptno
    Is there any example how to use 'first' and 'last' functions in an sql query ?
    I have tried to execute a query like this on the scott.emp table :
    select deptno,min(sal),max(sal),first(sal) from emp group by deptno;
    but I always get this message:
    ERROR at line 1:
    ORA-00904: "FIRST": invalid identifier
    btw I use Oracle RDBMS ver 9.1.0.3 for Solaris in my server.
    tx for your help
    sjarif

  • Jbo:DataScroller - how to ensure 'First' and 'Last' action links appear

    I'm working in JDeveloper 10.1.3.5.0.
    I'd like to know if there's a way to ensure that the action links 'First' and 'Last' will appear, in addition to the 'Next' and 'Previous' ones (that is, assuming we're not already at the first or last range position).
    It seems as if the thing that determines whether or not they appear is the number of total range positions. I can't be certain, but it seems like the magic number is 1000. If there are fewer, then 'First' and 'Last' will appear, and if there are more, then they will not. It'd be great to be able to have control of this, though. Is it possible?
    Thanks
    Shea

    Encore is being phased out?  I would be surprised if Adobe did not offer some kind of DVD authoring solution in it's place.  I have not heard about this yet myself.
    I have a menu size issue because, despite my reported file size being under 4.7G, Encore continues to report a menu size problem.  Of course it doesn't say which menu or by how much it is exceeded.  When I build it to DVD it waits several hours before reporting that there is a space issue.  See below.
    There is a file called woodstock-1.mp4.  I can find no use of this clip at this point and it's not part of a timeline, yet Encore won't let it be deleted.  Encore reports it's still in use.  Surely there should be some way to track down any dependencies quickly.

  • How to find FIRST and the LAST date of the month.

    Hello,
    I want to find the first and the last date of the current month through query. How is it possible please help.
    For example if the current month is july. The first date should be 01-JUL-2006 and the last date would be 31-JUL-2006.
    Please help me.
    Regards,
    Imran Baig

    Like this?
    SQL> select trunc(sysdate,'MM') "First_Day",
      2        last_day(sysdate) "Last_Day" from dual;
    First_Day Last_Day
    01-JUL-06 31-JUL-06
    SQL> select trunc(to_date('10-FEB-04'),'MM' ) "First_Day",
      2       last_day(to_date('10-FEB-04')) "Last_Day" from dual;
    First_Day Last_Day
    01-FEB-04 29-FEB-04

  • How  we claculate the first and last date of the month

    hi,
    I have a requirement, if i have the any date of a month like today date 02/13/2008. So i want the first date of this month and the last date of this month.
    Is there any FM for this..or if you have nay prorgam pls send to me.
    thanks.

    Hi Vipin,
    Try
    HR_JP_MONTH_BEGIN_END_DATE FM will return both first day and last day
    LAST_DAY_OF_MONTHS  for getting last day of month.
    Reward if helpful.
    Regards,
    Mandeep

  • First and Last date of any Month.

    Hi Friends,
    I have 2 fields in my table
    FROM DATE & TO DATE
    User can enter any date in these two fields, but at time of commit i want to change the FROM DATE to the first date of the Month user have entered. And for TO DATE i want to change the last date of the month.
    For Example :
    User Enters-
    FROM DATE 12-Mar-2003
    TO DATE 27-Jun-2003
    I want to change these date to:
    FROM DATE 01-Mar-2003
    TO DATE 30-Jun-2003
    How can I write a database function or procedure to control this thing.
    Please help.
    Best regards,
    Imran Baig

    It depends on your requirements, but I usually use TRUNC(date, 'D')
    to get the first day(Sunday) of the week, because TRUNC(date, 'W')
    returns the same day of the week as the first day of the month.
    See http://download-west.oracle.com/docs/cd/A87860_01/doc/server.817/a85397/function.htm#80101
    for more info on this.

Maybe you are looking for