Você está na página 1de 7

Report Studio

Cognos Time and Date Functions


CAST and EXTRACT Where [Time stamp] = Jun 8, 2009 5:26:47 PM Expression
cast(extract( year, [Time stamp]), varchar(4)) extract( hour, [Time stamp]) extract( day, [Time stamp]) cast(extract(month,[Time stamp]),VARCHAR(2)) cast([Time stamp], date) cast ([Time stamp], varchar(50)) cast(extract (hour,[Time stamp]), VARCHAR(2))

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)

11/5/2010

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 dates in prior month (based on system date):


extract(month, _add_months(current_date, -1)) = cast(extract(month,[Time stamp]),varchar(2))

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

11/5/2010

Page 2

Report Studio
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)))

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 YY 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 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 Previous month _age([BENEFICIARY_BIRTH_DATE]) / 100 extract(month, _add_months(current_date, -1))
subtract 24 months from the Time stamp month and display the resulting year.

Add/subtract months '20' || substrb(to_char(_add_months([Time stamp],-24)),8,2) This will

11/5/2010

Page 3

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')

11/5/2010

Page 4

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)))

11/5/2010

Page 5

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})))))

11/5/2010

Page 6

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

11/5/2010

Page 7

Você também pode gostar