Hi Gurus,
My requirement : One of my user enters a date which I will capture into a presentation variable v_date. Is there a way to calculate number of days in the v_date variable (or) the date entered by the user in OBIEE ?
TIA
Venkat
TIMESTAMPDIFF : https://docs.oracle.com/middleware/12213/biee/BIESQ/toc.htm#BIESQ415
Hi Christian,
TIMESTAMPDIFF needs following format .
TIMESTAMPDIFF(interval, timestamp1, timestamp2
Here I just have only one date.
Well yeah of course...you can't calculate a time range with one date...
But you said "Calculating no of days in a month", so it's pretty obvious that the start of the comparison has to be the first day of the month. Simple logic.
Hence the importance of a properly formed calendar dimension where every date has these types of attribute just laid down and there's no on-the-fly calculating ... very simple in ETL/ELT to do it and then no matter what date a use picks a simple lookup to the dimension returns the desired start, end, and days in between. Built it once and use it infinitely.
As @Christian Berg has said, it's pretty straightforward ... here's a start:
First Day of MonthTIMESTAMPADD(SQL_TSI_DAY, -DAYOFMONTH(CURRENT_DATE) +1 , CURRENT_DATE)
Last Day of MonthTIMESTAMPADD(SQL_TSI_DAY, -DAY(TIMESTAMPADD(SQL_TSI_MONTH, 1, CURRENT_DATE)), TIMESTAMPADD(SQL_TSI_MONTH, 1, CURRENT_DATE))
You might find this a useful reference; -
Functions:
Current_Date Current_TimeCurrent_TimeStampDay_Of_QuarterDayNameDayOfMonthDayOfWeekDayOfYearHourMinuteMonthMonth_Of_QuarterMonthNameNowQuarter_Of_YearSecondTimestampAddTimestampDiffWeek_Of_QuarterWeek_Of_YearYear
Current_Date
Returnsthe current date. The date is determined by the system in which the Oracle BIServer is running.
Syntax
Current_Time
Returnsthe current time. The time is determined by the system in which the Oracle BIServer is running.Note: The Analytics Server does not cache queries that contain thisfunction.
Current_Time(integer)
Where:
integer
Anyinteger that represents the number of digits of precision with which to displaythe fractional second.
Current_TimeStamp
Returnsthe current date/timestamp. The timestamp is determined by the system in whichthe Oracle BI Server is running.Note: The Oracle BI Server does not cache queries that contain thisfunction.
Current_TimeStamp(integer)
Day_of_Quarter
Returnsa number (between 1 and 92) corresponding to the day of the quarter for thespecified date.
Day_Of_Quarter(dateExpr)
dateExpr
Anyexpression that evaluates to a date.
DayName
Returnsthe name of the day for a specified date.
DayName(dateExpr)
DayOfMonth
Returnsthe number corresponding to the day of the month for a specified date.
DayOfMonth(dateExpr)
DayOfWeek
Returnsa number between 1 and 7 corresponding to the day of the week, Sunday throughSaturday, for a specified date. For example, the number 1 corresponds to Sundayand the number 7 corresponds to Saturday.
DayOfWeek(dateExpr)
DayOfYear
Returnsthe number (between 1 and 366) corresponding to the day of the year for aspecified date.
DayOfYear(dateExpr)
Hour
Returnsthe number (between 0 and 23) corresponding to the hour for a specified time.For example, 0 corresponds to 12 A.M. and 23 corresponds to 11 P.M.
Hour(timeExpr)
timeExpr
Anyexpression that evaluates to a time.
Minute
Returnsthe number (between 0 and 59) corresponding to the minute for a specified time.
Minute(timeExpr)
Month
Returnsa number (between 1 and 12) corresponding to the month for a specified date.
Month(dateExpr)
Month_Of_Quarter
Returnsthe number (between 1 and 3) corresponding to the month in the quarter for aspecified date.
Month_Of_Quarter(dateExpr)
MonthName
Returnsthe name of the month for a specified date.
MonthName(dateExpr)
Now
Returnsthe current timestamp. This function is equivalent to the functioncurrent_timestamp .
Now()
Quarter_Of_Year
Returnsthe number (between 1 and 4) corresponding to the quarter of the year for aspecified date.
Quarter_Of_Year(dateExpr)
Second
Returnsthe number (between 0 and 59) corresponding to the seconds for a specifiedtime.
Second(timeExpr)
TimestampAdd
Addsa specified number of intervals to a specified timestamp, and returns a singletimestamp. Passing a null intExpr or timeExpr to this function results in thereturn of a null value.In the simplest scenario, this function simply adds the specified integer valueto the appropriate component of the timestamp, based on the interval. Adding aweek translates to adding seven days, and adding a quarter translates to addingthree months. A negative integer value results in a subtraction (such as goingback in time).An overflow of the specified component (for example, more than 60 seconds, 24hours, twelve months, and so on) necessitates adding an appropriate amount tothe next component. For example, when adding to the day component of atimestamp, this function makes sure that overflow takes into account the numberof days in a particular month (including leap years). Similar measures are usedto make sure that adding a month component results in the appropriate number ofdays for the day component (such as adding a month to '2010-05-31' does notresult in '2010-06-31' because June does not have 31 days). The function alsodeals with the month and day components in a similar fashion when adding orsubtracting year components.
TimestampAdd(interval,intExpr, timestamp)
interval
Thespecified interval. Valid values are: SQL_TSI_SECOND, SQL_TSI_MINUTE,SQL_TSI_HOUR, SQL_TSI_DAY, SQL_TSI_WEEK, SQL_TSI_MONTH, SQL_TSI_QUARTER,SQL_TSI_YEAR.
intExpr
Anyexpression that evaluates to an integer value.
timestamp
Anyvalid timestamp.
Examples
Select{TimestampAdd(SQL_TSI_DAY, 3, TIMESTAMP'2000-02-27 14:30:00')}From Employee where employeeid = 2;
Inthe above example, the query asks for the resulting timestamp when 3 days areadded to '2000-02-27 14:30:00'. Since February, 2000 is a leap year, the queryreturns a single timestamp of '2000-03-01 14:30:00'.
Select{TimestampAdd(SQL_TSI_MONTH, 7, TIMESTAMP'1999-07-31 00:00:00')}From Employee where employeeid = 2;
Inthe above example, the query asks for the resulting timestamp when 7 months areadded to '1999-07-31 00:00:00'. The query returns a single timestamp of'2000-02-29 00:00:00'. Notice the reduction of day component to 29 because ofthe shorter month of February.
Select{TimestampAdd(SQL_TSI_MINUTE, 25, TIMESTAMP'2000-07-31 23:35:00')}From Employee where employeeid = 2;
Inthe above example, the query asks for the resulting timestamp when 25 minutesare added to '2000-07-31 23:35:00'. The query returns a single timestamp of'2000-08-01 00:00:00'. Notice the propagation of overflow through the monthcomponent.
TimestampDiff
Returnsthe total number of specified intervals between two timestamps. Passing a nulltimestamp to this function results in a null return value.This function first determines the timestamp component that corresponds to thespecified interval parameter, and then looks at the higher order components ofboth timestamps to calculate the total number of intervals for each timestamp.For example, if the specified interval corresponds to the month component, thefunction calculates the total number of months for each timestamp by adding themonth component and twelve times the year component. Then the functionsubtracts the first timestamp's total number of intervals from the secondtimestamp's total number of intervals.The TimestampDiff function rounds up to the next integer whenever fractionalintervals represent a crossing of an interval boundary. For example, thedifference in years between '1999-12-31' and '2000-01-01' is 1 year because thefractional year represents a crossing from one year to the next (such as 1999to 2000). By contrast, the difference between '1999-01-01' and '1999-12-31' iszero years because the fractional interval falls entirely within a particularyear (such as 1999). Microsoft's SQL Server exhibits the same roundingbehavior, but IBM's DB2 does not; it always rounds down. Oracle does not implementa generalized timestamp difference function.When calculating the difference in weeks, the function calculates thedifference in days and divides by seven before rounding. Additionally, thefunction takes into account how the administrator has configured the start of anew week in the NQSConfig.ini file. For example, with Sunday as the start ofthe week, the difference in weeks between '2000-07-06' (a Thursday) and'2000-07-10' (the following Monday) results in a value of 1 week. With Tuesdayas the start of the week, however, the function would return zero weeks sincethe fractional interval falls entirely within a particular week. Whencalculating the difference in quarters, the function calculates the differencein months and divides by three before rounding. Oracle BI Server pushes down the TIMESTAMPADD and TIMESTAMPDIFF functions toMicrosoft's SQL Server and ODBC databases by default. While Oracle BI Servercan also push to IBM's DB2, the features table is turned off by default due toDB2's simplistic semantics. (IBM's DB2 provides a generalized timestampdifference function, TIMESTAMPDIFF, but it simplifies the calculation by alwaysassuming a 365-day year, 52-week year, and 30-day month.) The features table isalso turned off by default for Oracle, since Oracle databases do not fullysupport these functions.
TimestampDiff(interval,timestamp1, timestamp2)
timestamp1
timestamp2
Select{TimestampDiff(SQL_TSI_DAY, TIMESTAMP'1998-07-31 23:35:00',TIMESTAMP'2000-04-01 14:24:00')}From Employee where employeeid = 2;
Inthe above example, the query asks for a difference in days between timestamps'1998-07-31 23:35:00' and '2000-04-01 14:24:00'. It returns a value of 610.Notice that the leap year in 2000 results in an additional day.
Week_Of_Quarter
Returnsa number (between 1 and 13) corresponding to the week of the quarter for thespecified date.
Week_Of_Quarter(dateExpr)
Week_Of_Year
Returnsa number (between 1 and 53) corresponding to the week of the year for thespecified date.
Week_Of_Year(dateExpr)
Year
Returnsthe year for the specified date.
Year(dateExpr)
dateExprAny expression that evaluates to a date.