Cognos Time and Date Functions
Cognos Time and Date Functions
Result
2009 (alpha) 17 (numeric) 8 (numeric) 6 (alpha note lack of leading zero) Jun 8, 2009 (date is a data type) 2009-06-08 00:00:00.000000000 5 (alpha)
Extract minute as two digit alpha: case (CAST(extract ( minute, [Audit].[Run Reports].[Time stamp]), VARCHAR(2))) when '0' then ('00') when '1' then ('01') when '2' then ('02') when '3' then ('03') when '4' then ('04') when '5' then ('05') when '6' then ('06') when '7' then ('07') when '8' then ('08') when '9' then ('09') else (CAST(extract ( minute, [Audit].[Run Reports].[Time stamp]), VARCHAR(2))) end or TO_CHAR([Time stamp], MM) Last Day of Current Month _last_of_month(date2timestamp(Today())) Date Minus 24 Hours _add_days([Audit].[COGIPF_RUNREPORT].[TIME STAMP],-1)
8/7/2012
Page 1
Report Studio
Use a date in a filter: [Audit].[Run Reports].[Time stamp] > cast('2010-05-01', timestamp) or
cast ([CURRENT_HIRE_DATE], varchar(50)) > '2005-11-10 00:00:00.000000000'
Note: Strangely, [Audit].[Run Reports].[Time stamp] = cast('2010-05-01', timestamp) does not work as a filter. However, the following does work:
[Time stamp] between (cast('2010-05-10', timestamp)) and (cast('2010-05-11', timestamp))
To restrict a report based on a hard coded date (ex. 5/10/2010): cast ([Time stamp], varchar(50)) = '2010-05-10 00:00:00.000000000'
To filter records based on a timestamp when using date prompts on a prompt page: Filter1: [TIME STAMP] >= cast((?beginDate?) as TIMESTAMP) Filter2: [TIME STAMP] <= _add_days(cast((?endDate?) as TIMESTAMP),1) (A day is added to the end date to allow for the use of a single day range. That is, the beginDate will be Apr 27, 2012 12:00:00 AM if April 27 is selected. If April 27 is also selected as the end date, 24 hours must be added so that the end date used in the filter is Apr 28, 2012 12:00:00 AM)
8/7/2012
Page 2
Report Studio
SYSDATE This CASE function extracts the first three characters of the current date and translates it into a fiscal period: CASE (substr({sysdate},4,3)) WHEN 'JUL' THEN '01' WHEN 'AUG' THEN '02' WHEN 'SEP' THEN '03' WHEN 'OCT' THEN '04' WHEN 'NOV' THEN '05' WHEN 'DEC' THEN '06' WHEN 'JAN' THEN '07' WHEN 'FEB' THEN '08' WHEN 'MAR' THEN '09' WHEN 'APR' THEN '10' WHEN 'MAY' THEN '11' WHEN 'JUN' THEN '12' ELSE '14' END
To calculate the fiscal year based on the current date (fiscal year for 2009/2010 is 2010): IF (extract( month, {sysdate}) < 7) THEN (cast(extract(year, {sysdate}), varchar(4))) ELSE (cast(extract(year, {sysdate})+1, varchar(4)))
This function will return the date of the same day of the week a year ago and controls for leap years. For example, if today is Tuesday, 07/31/2012 then the function returns 08/02/2012 (Tuesday) trunc removes the time portion of the date value: trunc(_add_days({sysdate},-364))
8/7/2012
Page 3
Report Studio
TO_CHAR General format is TO_CHAR(datetime, format element) Example: TO_CHAR(current_date, YYYYMM) = 201012 (December, 2010) Element D DD DAY HH HH24 MI MM MONTH MON SS WW W YYYY Description Number of day of the week Number of day of the month Name of the day (ex. FRIDAY) Hour of day (1-12) Hour of day (0-23) Minute (0-59) Month (January = 01December = 12) Name of month Abbreviated name of month Seconds Week of year (1-53) Week of month (1-5 where week 1 starts on first day of month and ends on seventh) 4-digit year (YY returns last two digits of 4-digit year)
Year and Month in YYYYMM format: Current year/month to_char(current_date,'YYYYMM') ex: 200910 Previous month to_char(add_months(current_date,-1),'YYYYMM') ex: 200909 Previous year/month- to_char(add_months(current_date,-13),'YYYYMM') ex: 200809
OTHER Age in years Age in years _age([BENEFICIARY_BIRTH_DATE]) / 100 _years_between(current_date,[Date of Birth]) returns number of full years between the two dates
Age at time of graduation in years and fractional months _months_between ([OUTCOME_GRADUATION_DATE],[BIRTH_DATE])/12 returns number of full years and fractional months (ex: 21.4) as of the date of graduation Previous month extract(month, _add_months(current_date, -1))
subtract 24 months from the Time stamp month and display the resulting year.
8/7/2012
Page 4
Report Studio
TRUNC (supplied by H. Cleveland) TRUNC(date, [format]) Where [format] is optional and can be any of the following: Year ISO Year Quarter Month Week IW W Day Start day of the week Hour Minute SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y IYYY, IY, I Q MONTH, MON, MM, RM WW IW W DDD, DD, J DAY, DY, D HH, HH12, HH24 MI
Examples: Start of today: Code: trunc({sysdate}) Start of yesterday: Code: (trunc(_add_days({sysdate}, -1),'dd')) Start of Current Month: Code: trunc({sysdate},'mm') End of Current Month: Code: last_day({sysdate}) Start of Previous Month: Code: trunc((trunc({sysdate},'mm')-1),'mm') End of Previous Month: Code: trunc({sysdate},'mm')-1 Start of Current Quarter: Code: trunc({sysdate},'q') End of Current Quarter: Code: add_months(trunc({sysdate},'q'),3)-1 Start of Previous Quarter: Code: trunc(trunc({sysdate},'q')-1,'q')
8/7/2012
Page 5
Report Studio
End of Previous Quarter: Code: trunc({sysdate},'q')-1 Start of Current Year: Code: trunc({sysdate},'y') End of Current Year: Code: add_months(trunc({sysdate},'y'),12)-1 Start of Previous Year: Code: trunc(trunc({sysdate},'y')-1,'y') End of Previous Year: Code: trunc({sysdate},'y')-1
When [TIME STAMP] = Aug 3, 2010 5:05:45 PM Start of day in [TIME STAMP]: trunc(_add_days([TIME STAMP], 0),'dd') = Aug 3, 2010 12:00:00 AM Start of day previous to day in [TIME STAMP]: trunc(_add_days([TIME STAMP],-1),'dd') = Aug 2, 2010 12:00:00 AM Start of the hour in [TIME STAMP]; trunc([TIME STAMP], 'hh') = Aug 3, 2010 5:00:00 PM All records that occurred yesterday: [TIME STAMP] between (trunc(_add_days({sysdate}, -1),'dd')) and (trunc({sysdate})) Day of week for current date:
Code: _day_of_week(current_date , 1)
If it's Monday, include Decision Dates = Sat, Sun or Mon otherwise DECISION DATE = Current Date: (((_day_of_week(current_date,1) = 1 ) AND ([Admission Application].[Admissions Application].[LATEST_DECISION_DATE] BETWEEN (current_date - 2) AND current_date)) OR ((_day_of_week(current_date,1) <> 1) AND ([Admission Application].[Admissions Application].[LATEST_DECISION_DATE] = current_date)))
8/7/2012
Page 6
Report Studio
If its Monday, then include any record beginning with the start of Friday through the start of Monday, otherwise include any record beginning with the start of yesterday through the start of Today: ((_day_of_week(current_date,1) = 1 ) AND ([Audit].[COGIPF_RUNREPORT].[TIME STAMP] BETWEEN (trunc(_add_days({sysdate}, -3),'dd')) AND (trunc({sysdate}))) OR ((_day_of_week(current_date,1) <> 1) AND ([Audit].[COGIPF_RUNREPORT].[TIME STAMP] BETWEEN (trunc(_add_days({sysdate}, -1),'dd')) AND (trunc({sysdate})))))
8/7/2012
Page 7
Report Studio
TRUNC (timestamp, [parts of timestamp]) Where [parts of timestamp] can be 'D'-- Return only day information in the timestamp. Hours, minutes, and seconds are returned as zero. 'h'-- Return only day and hour information in the timestamp. Minutes and seconds are returned as zero. 'm'-- Return only day, hour, and minute information in the timestamp. Seconds are returned as zero. 's'-- Return only day, hour, and second information in the timestamp, but do not show milliseconds.
TRUNC also can be used with decimal numbers to return a number rounded to a given number of decimal places. For example: TRUNC(1234.567) returns 1,234 TRUNC(1234.567, 1) returns 12,345.6 TRUNC(1234.567, -2) returns 1,200
8/7/2012
Page 8