Author Topic: Extract Year from Date Data Item  (Read 318 times)

Offline t0mato

  • Full Member
  • ***
  • Join Date: Aug 2020
  • Posts: 48
  • Forum Citizenship: +0/-0
Extract Year from Date Data Item
« on: 08 Sep 2021 12:23:56 pm »
Hello!

I have a data item called [Date] that returns a date (full string date e.g. 01/03/2021). I want to return 2021. I know of the expression month() but how can I get the year from this in a data item expression? Should I just duplicate the data item and display it as YYYY by changing the data format?

Offline MFGF

  • Never knowingly correct
  • Super Moderator
  • Statesman
  • ******
  • Join Date: Jul 2005
  • Posts: 11,376
  • Forum Citizenship: +665/-10
  • Cognos Software Muppet
Re: Extract Year from Date Data Item
« Reply #1 on: 08 Sep 2021 12:56:12 pm »
Hello!

I have a data item called [Date] that returns a date (full string date e.g. 01/03/2021). I want to return 2021. I know of the expression month() but how can I get the year from this in a data item expression? Should I just duplicate the data item and display it as YYYY by changing the data format?

Hi,

If you need just the year value on its own, you can use the extract() function to get it, eg

extract(year, [Date])

This is useful if you need to use the year in a calculation or filter, or to group on the year in your report. If none of these apply, you could use formatting to hide the day and month parts, but be aware the item will still contain a full date on each row under the covers.

Cheers!

MF.
Meep!

Offline t0mato

  • Full Member
  • ***
  • Join Date: Aug 2020
  • Posts: 48
  • Forum Citizenship: +0/-0
Re: Extract Year from Date Data Item
« Reply #2 on: 09 Sep 2021 03:26:14 pm »
Hi,

If you need just the year value on its own, you can use the extract() function to get it, eg

extract(year, [Date])

This is useful if you need to use the year in a calculation or filter, or to group on the year in your report. If none of these apply, you could use formatting to hide the day and month parts, but be aware the item will still contain a full date on each row under the covers.

Cheers!

MF.

This is exactly what I needed. I was using the RIGHT function instead but this appears to literally be built for working with dates. Thank you!