0% found this document useful (0 votes)
56 views5 pages

Cognos

The document describes various time and date functions in Cognos that can be used to extract, format, and manipulate date and timestamp values. It provides examples of using functions like CAST, EXTRACT, TRUNC, and ADD_MONTHS to get specific parts of dates like year, month, day, and hour. It also demonstrates how to filter reports and calculations based on dates, calculate ages from birthdates, and determine fiscal periods from system dates.

Uploaded by

Harry Konnect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views5 pages

Cognos

The document describes various time and date functions in Cognos that can be used to extract, format, and manipulate date and timestamp values. It provides examples of using functions like CAST, EXTRACT, TRUNC, and ADD_MONTHS to get specific parts of dates like year, month, day, and hour. It also demonstrates how to filter reports and calculations based on dates, calculate ages from birthdates, and determine fiscal periods from system dates.

Uploaded by

Harry Konnect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Report Studio

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

Last Day of Current Month

_last_of_month(date2timestamp(Today()))

Date Minus 24 Hours

_add_days([Audit].[COGIPF_RUNREPORT].[TIME STAMP],-1)

Use a date in a filter:

[Audit].[Run Reports].[Time stamp] > cast('2010-05-01', timestamp)

8/4/2010 Page 1
Report Studio

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

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

8/4/2010 Page 2
Report Studio

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.

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

8/4/2010 Page 3
Report Studio

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

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

8/4/2010 Page 4
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/4/2010 Page 5

You might also like