Subtracting or adding Dates in your CAML quey filters

I had a link list on a WSS v3 site and i wanted to  show this list on other sites and subsites.
So i decided to plug to the datasource and extract the links using a DataView Webpart.
It all went well and good but i wanted another view that will show  users latest links added a week ago for example .

So of course decided to use a filter using the
Field Name - Created 
Comparison - Greater that or Equal to
Value - [CurrentDate] 

But there was no way to change the CurrentDate from the Filter Criteria Menu because i need to do
CurrentDate - 5 (which means subract 5 days  from the the CurrentDate.
Well i said to myself what left THE CODE

My first approach was use this caml query like so below but didn't work

<View><Query>
<OrderBy><FieldRef Name="Column3" Ascending="TRUE"/>
</OrderBy>
<Where>
<Geq>
<FieldRef Name="Modified"/>
<Value Type="Text">
<[Today]-4]/>
</Value>
</Geq>
</Where>
</Query>
</View>
 

Until i found out i could use an OffsetDays attribute like below :

<Value Type="DateTime"><Today OffsetDays="-8"/></Value>


Which means that 8 is subtracted from the date that the query filters on.

And that did the trick


<SharePoint:SPDataSource runat="server" SelectCommand="&lt;View&gt;&lt;Query&gt;&lt;OrderBy&gt;&lt;FieldRef Name=&quot;Column3&quot; Ascending=&quot;TRUE&quot;/&gt;&lt;/OrderBy&gt;&lt;Where&gt;&lt;Geq&gt;&lt;FieldRef Name=&quot;Created&quot;/&gt;&lt;Value Type=&quot;Text&quot;&gt;&lt;Today OffsetDays=&quot;-8&quot;/&gt;&lt;/Value&gt;&lt;/Geq&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;"

posted @ Tuesday, November 20, 2007 4:27 PM

Print

Comments on this entry:

# re: Subtracting or adding Dates in your CAML quey filters

Left by Frank at 12/26/2007 3:26 PM
Gravatar
It's incredible, but it's very useful!
Thanks for sharing!

# re: Subtracting or adding Dates in your CAML quey filters

Left by Chris at 2/25/2008 10:51 PM
Gravatar
How do you add the queries into the SharePoint site?

I do not really want to type the code view one i.e. &quot;/&gt.... etc.

# re: Subtracting or adding Dates in your CAML quey filters

Left by Patrick at 2/26/2008 11:56 AM
Gravatar
Chris you can use sharepoint designer and plug into the code view to do this.Just follow the instructions above and you should be fine.

# re: Subtracting or adding Dates in your CAML quey filters

Left by srikanth at 11/3/2008 8:37 PM
Gravatar
it's very nice.........(very useful)

# Cub caduet lawn more blades

Left by Tammy at 11/23/2009 12:07 PM
Gravatar
Good Day. You never know how a horse will pull until you hook him up to a heavy load. Help me! Could you help me find sites on the: Cub caduet lawn more blades. I found only this - caduet and incontinence side effects. Vectormune fp-mg injectable, caduet. The prescription of obstruction pointed entirely established, caduet. THX :confused:, Tammy from Honduras.

Your comment:



 (will not be displayed)


 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345