Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to create new view for my list. The list contains a column called Year, which is a number and contains, as name suggests, year. Eg.:

2012
2011
2010
2009

I would like create a view, where I would display only last 3 years. The <Query> element of my List Definition is:

<Query>
  <Where>
    <Geq>
      <FieldRef Name="Year" />
      <Value Type="Number"> ???? </Value>
    </Geq>
  </Where>
</Query>

And it gets tricky here. How to write a condition, that a number should be greater that current year minus 3? I thought it would work like in UI, so I could use:

YEAR([Today])-3
share|improve this question

2 Answers 2

Maybe you can you use the OffsetDays attribute for this? (all depends on how exact the 3 years should be) as there is no way you can do leap years this way. Here is how do it using days in case.

<Query>
  <Where>
    <Geq>
      <FieldRef Name="Year" />
         <Today OffsetDays="-1" /> //amount of days to subtract
    </Geq>
  </Where>
</Query>

Hope this helps

share|improve this answer

What I ended up doing was to create additional calculated column that returns either "yes" or "no" values. In your case the formula would be similar to:

=IF([Year]>=TEXT(YEAR([Today])-3,"0"),"Yes","No")

In order to be able to use [Today] in the formula you need to create a single line of text field called "Today". After your calculated column is created remove the "Today" column (here is the original post for the process).

Once you have the calculated column created, your query would look similar to this:

<Query>
  <Where>
    <Geq>
      <FieldRef Name="<Calc Field Here>" Type="Choice"/>
        <Value Type="Choice">Yes</Value>
    </Geq>
  </Where>
</Query>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.