cognos time and date functions

7
Report Studio 11/5/2010 Page 1 Cognos Time and Date Functions CAST and EXTRACT Where [Time stamp] = Jun 8, 2009 5:26:47 PM Expression Result cast(extract( year, [Time stamp]), varchar(4)) 2009 (alpha) extract( hour, [Time stamp]) 17 (numeric) extract( day, [Time stamp]) 8 (numeric) cast(extract(month,[Time stamp]),VARCHAR(2)) 6 (alpha note lack of leading zero) cast([Time stamp], date) Jun 8, 2009 (‘date’ is a data type) cast ([Time stamp], varchar(50)) 2009-06-08 00:00:00.000000000 cast(extract (hour,[Time stamp]), VARCHAR(2)) 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)

Upload: joao-matos

Post on 02-Oct-2014

1.082 views

Category:

Documents


11 download

TRANSCRIPT

Page 1: Cognos Time and Date Functions

Report Studio

11/5/2010 Page 1

Cognos Time and Date Functions

CAST and EXTRACT

Where [Time stamp] = Jun 8, 2009 5:26:47 PM

Expression Result

cast(extract( year, [Time stamp]), varchar(4)) 2009 (alpha)

extract( hour, [Time stamp]) 17 (numeric)

extract( day, [Time stamp]) 8 (numeric)

cast(extract(month,[Time stamp]),VARCHAR(2)) 6 (alpha – note lack of leading zero)

cast([Time stamp], date) Jun 8, 2009 (‘date’ is a data type)

cast ([Time stamp], varchar(50)) 2009-06-08 00:00:00.000000000

cast(extract (hour,[Time stamp]), VARCHAR(2)) 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)

Page 2: Cognos Time and Date Functions

Report Studio

11/5/2010 Page 2

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

Page 3: Cognos Time and Date Functions

Report Studio

11/5/2010 Page 3

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 Description D Number of day of the week DD Number of day of the month DAY Name of the day (ex. FRIDAY) HH Hour of day (1-12) HH24 Hour of day (0-23) MI Minute (0-59) MM Month (January = 01…December = 12) MONTH Name of month MON Abbreviated name of month SS Seconds WW Week of year (1-53)

W Week of month (1-5 where week 1 starts on first day of month and ends on seventh)

YYYY 4-digit year YY 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([BENEFICIARY_BIRTH_DATE]) / 100 Previous month extract(month, _add_months(current_date, -1))

Add/subtract months '20' || substrb(to_char(_add_months([Time stamp],-24)),8,2) – This will subtract 24 months from the Time stamp month and display the resulting year.

Page 4: Cognos Time and Date Functions

Report Studio

11/5/2010 Page 4

TRUNC (supplied by H. Cleveland)

TRUNC(date, [format]) Where [format] is optional and can be any of the following:

Year SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y ISO Year IYYY, IY, I Quarter Q Month MONTH, MON, MM, RM Week WW IW IW W W Day DDD, DD, J Start day of the week DAY, DY, D Hour HH, HH12, HH24 Minute 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')

Page 5: Cognos Time and Date Functions

Report Studio

11/5/2010 Page 5

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

Page 6: Cognos Time and Date Functions

Report Studio

11/5/2010 Page 6

If it’s 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})))))

Page 7: Cognos Time and Date Functions

Report Studio

11/5/2010 Page 7

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