Sunteți pe pagina 1din 3

OBIEE Date Expressions Reference During a recent project I had several requirements that involved converting dates to the

first or last day of the current month, past month, future month etc. Thanks to that project Ive become proficient at Date manipulations and thought I would share some of the expressions I had to use on the project and how they worked. For people who have not worked a lot with OBIEE Date manipulation this may come in handy since OBIEE does not offer built in functions like Oracle which has the LAST_DAY function to return the last day of a given month. Any date manipulation within OBIEE will utilize the TIMESTAMPADD function. For the examples shown in the table below I used CURRENT_DATE as the starting point for all dates. You can replace CURRENT_DATE with whatever valid Date type you need as a starting point or can CAST a CHAR value to a Date type if needed. The table below describes what is being calculated, the expression to perform the calculation, and then an explanation that explains how the expression arrives at the desired date value. Date Calculation OBIEE Expression Explanation

First Day of the Previous Year First Day of the Current Year

From right to left the first TIMESTAMPADD returns the first day of TIMESTAMPADD( SQL_TSI_YEAR , the current year. The second -1,TIMESTAMPADD( SQL_TSI_DAY TIMESTAMPADD removes a year from ,EXTRACT( DAY_OF_YEAR FROMCURRE the returned date for the First Day of the NT_DATE) * -(1) + 1,CURRENT_DATE)) Previous Year. TIMESTAMPADD( SQL_TSI_DAY This calculation returns the first day of ,EXTRACT( DAY_OF_YEAR FROMCURRE the year by deducting one less than the NT_DATE) * -(1) + 1,CURRENT_DATE) total number of days in the year.

From right to left the first TIMESTAMPADD returns the first day of TIMESTAMPADD( SQL_TSI_YEAR , the current year. The second 1,TIMESTAMPADD( SQL_TSI_DAY TIMESTAMPADD adds a year to the First Day of the Next ,EXTRACT( DAY_OF_YEAR FROMCURRE date returned which will give the first day Year NT_DATE) * -(1) + 1,CURRENT_DATE)) of the next year. From right to left the first TIMESTAMPADD returns the first day of the Current Month. The second TIMESTAMPADD(SQL_TSI_MONTH, -1, TIMESTAMPADD then subtracts one TIMESTAMPADD( SQL_TSI_DAY month from the first day of the Current ,DAYOFMONTH( CURRENT_DATE) * -(1) + Month arriving to the First Day of the 1, CURRENT_DATE)) previous month. This expression gets the current day of TIMESTAMPADD( SQL_TSI_DAY the month and subtracts one less than ,DAYOFMONTH( CURRENT_DATE) * -(1) + the current day to arrive at the first day of 1, CURRENT_DATE) the month.

First Day of the Previous Month

First Day of the Current Month

From right to left the first TIMESTAMPADD returns the first day of the Current Month. The second TIMESTAMPADD(SQL_TSI_MONTH, 1, TIMESTAMPADD then adds one month TIMESTAMPADD( SQL_TSI_DAY from the first day of the Current Month First Day of the Next ,DAYOFMONTH( CURRENT_DATE) * -(1) + arriving to the First Day of the next Month 1, CURRENT_DATE)) month. This was included to show the calculations discussed above can be used with other functions. This is the same expression as the one that returns the first day of the current month except TIMESTAMPADD( SQL_TSI_DAY this one uses the DAY_OF_QUARTER ,DAY_OF_QUARTER( CURRENT_DATE) * property to return the first day of the -(1) + 1, CURRENT_DATE) current quarter. From right to left the first TIMESTAMPADD returns the first day of TIMESTAMPADD( SQL_TSI_DAY , the Current Month. The second (1),TIMESTAMPADD( SQL_TSI_DAY TIMESTAMPADD subtracts a month to ,DAYOFMONTH( CURRENT_DATE) * -(1) + arrive at the first day of the previous 1, CURRENT_DATE)) month. From right to left the first TIMESTAMPADD finds the first day of the current Month. The second TIMESTAMPADD adds one month to the TIMESTAMPADD( SQL_TSI_DAY , date to arrive at the first day of the next (1),TIMESTAMPADD( SQL_TSI_MONTH , month. The final TIMESTAMPADD 1,TIMESTAMPADD( SQL_TSI_DAY subtracts one day from the returned date ,DAYOFMONTH( CURRENT_DATE) * -(1) + to arrive at the last day of the Current 1, CURRENT_DATE))) Month.

First Day of Current Quarter

Last Day of the Previous Month

Last Day of Current Month

From right to left the first TIMESTAMPADD finds the first day of the current Month. The second TIMESTAMPADD adds two months to TIMESTAMPADD( SQL_TSI_DAY , the date to arrive at the first day of month (1),TIMESTAMPADD( SQL_TSI_MONTH , after next. The final TIMESTAMPADD 2,TIMESTAMPADD( SQL_TSI_DAY subtracts one day from the returned date Last Day of the Next ,DAYOFMONTH( CURRENT_DATE) * -(1) + to arrive at the last day of the Next Month 1, CURRENT_DATE))) Month.

From right to left the first TIMESTAMPADD returns the first day of TIMESTAMPADD( SQL_TSI_DAY , the current year. The second -1,TIMESTAMPADD( SQL_TSI_DAY TIMESTAMPADD subtracts one day to Last Day of Previous ,EXTRACT( DAY_OF_YEAR FROMCURRE arrive at December 31st of the previous Year NT_DATE) * -(1) + 1,CURRENT_DATE)) year. From right to left the first TIMESTAMPADD returns the first day of the current year. The second TIMESTAMPADD(SQL_TSI_YEAR, 1, TIMESTAMPADD deducts one day to TIMESTAMPADD( SQL_TSI_DAY , arrive at December 31 of the previous -1,TIMESTAMPADD( SQL_TSI_DAY year. The third TIMESTAMPADD adds a ,EXTRACT( DAY_OF_YEAR FROMCURRE single year to the date to arrive at NT_DATE) * -(1) + 1,CURRENT_DATE))) December 31 of the Current Year.

Last Day of Current Year

From right to left the first TIMESTAMPADD returns the first day of the current year. The second TIMESTAMPADD(SQL_TSI_YEAR, 2, TIMESTAMPADD deducts one day to TIMESTAMPADD( SQL_TSI_DAY , arrive at December 31 of the previous -1,TIMESTAMPADD( SQL_TSI_DAY year. The third TIMESTAMPADD adds 2 Last Day of the Next ,EXTRACT( DAY_OF_YEAR FROMCURRE years to the date to arrive at December Year NT_DATE) * -(1) + 1,CURRENT_DATE))) 31 of the Next Year. Demonstrated using Quarters. From right to left the first TIMESTAMPADD returns the first day of the Current Quarter. The TIMESTAMPADD( SQL_TSI_DAY , second TIMESTAMPADD returns the (1),TIMESTAMPADD( SQL_TSI_QUARTER first day of the next quarter. The final , 1, TIMESTAMPADD( SQL_TSI_DAY TIMESTAMPADD subtracts a single day Last Day of Current ,DAY_OF_QUARTER( CURRENT_DATE) * from the date to arrive at the last day of Quarter -(1) + 1, CURRENT_DATE))) the Current Quarter. For simplicity I hard coded the January 1, 2010 date and CAST it to a date. I could have used the First Day of the Current Year calculation but didnt want to over TIMESTAMPDIFF(SQL_TSI_DAY, clutter the example. The second part of CAST('2010/01/01 00:00:00' AS DATE), the TIMESTAMPDIFF uses Last Day of TIMESTAMPADD( SQL_TSI_DAY , the Current Month calculation to force Number of days (1),TIMESTAMPADD( SQL_TSI_MONTH , the TIMESTAMPDIFF to calculate the between First Day of 1,TIMESTAMPADD( SQL_TSI_DAY number of days between the first day of Year and Last Day of ,DAYOFMONTH( CURRENT_DATE) * -(1) + the year and the last day of the current Current Month 1, CURRENT_DATE)))) month.

S-ar putea să vă placă și