Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FilterExpression by year of date 1

Status
Not open for further replies.

dstrange

Programmer
Nov 15, 2006
87
CA
Hello All,

In code I'm trying to use a filterexpression on a datetime field only by the year.

ex.
datasource.FilterExpression = "year[postDate] = '2007'"

In sql I can use:
select * from tblTable where year(datefield) = '2007'
and it works.
Any ideas? Thanks in advance.
 
Code:
var january_1st = new DateTime(year, 1, 1);
var december_31st = new DateTime(year, 12, 31);
var filter = string.Format("year >= {0} and year <= {1}", january_1st, december_31st);
datasource.FilterExpression = filter;


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Can I use the function year to extract the year on a date in the filterexpression?

I get the error below when I try this filterexpression:
datasource.FilterExpression = "year[datefield] = '2007'"

"The expression contains undefined function call year(). "
 
the exception is telling you what the problem is. year() is undefined. I would assume that means; no, I cannot use functions within the expression.

again, why not just look for dates between janauary 1st and december 31st?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I'm trying but still having no luck....
Code:
Dim january_1st As New DateTime(rsNews.Item("yearDate"), 1, 1)
Dim december_31st As New DateTime(rsNews.Item("yearDate"), 12, 31)

Dim filterstr As String = String.Format("postdate >= #{0}# and postdate <= #{1}#", january_1st.ToString, december_31st.ToString)

sLatestNews.FilterExpression = filterstr

String was not recognized as a valid DateTime.
 
Code:
Dim filterstr As String = String.Format("[postdate] >= #{0}# and [postdate] <= #{1}#", january_1st, december_31st)
or
Code:
Dim filterstr As String = String.Format("[postdate] >= #01/01/{0}# and [postdate] <= #12/31/{0}#", year)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top