tags:

views:

182

answers:

4

If i have a Date/Time field in Access and i need to find all the entries that are in a specific year... say 2009. If my fields are set up as MM/DD/YYYY, how would i do a query to essentially do //2009

update Thanks for the help everyone, but i guess the issue is that I'm kind of dynamically constructing my SQL statement in ASP.

"SELECT * FROM database WHERE (datecolumn LIKE " & string & ")" where the string ends up returning a string i constructed 'DD/MM/YYYY'. If the user inputs nothing into either of the 3 fields, i'll needa do a specific query where that part is a wildcard... as in if they don't put in a day, i'd essentially need a

"SELECT * FROM database WHERE (datecolumn LIKE "*/MM/YY")".... which doesnt work in microsoft access

A: 

OTTOMH

SELECT * FROM Table WHERE DatePart ("yyyy", YourDatetimeColumn) = 2009

Raj More
This is a bad answer because first of all, it won't use indexes, and second it's not database-engine agnostic.
David-W-Fenton
Indexes - you are right. DB engine agnostic - LOL! the question specifies "in ACCESS" - he has to use Access specific queries!
Raj More
+5  A: 

How about

WHERE MyDate >= '1/1/2009' AND MyDate < '1/1/2010'
Bob Kaufman
...AND MyDate < 1/1/2010 or you lose the 31st
Paul
@Paul -- although I'm technically correct, yours is a best-practice. I'm making the dangerous assumption that the hours:minutes:seconds will all contain zeros. My answer has been edited accordingly.
Bob Kaufman
+1 because (at least in Sybase) using year() or any other functionon a field in the where clause will prevent the correct index on that field to be used.
DVK
+1, will still use an index
KM
if you want all of 2009, wouldn't it be _WHERE MyDate>='1/1/2009' AND MyDate<'1/1/2010', you don't want to get rows that happened at 1/1/2010 00:00:00.000
KM
@KM you are correct, and that's what I *thought* I'd typed! DFarn MS ergo keyboard. Answer corrected accordingly.
Bob Kaufman
+3  A: 

You should be able to use the Year() function

Some Examples:

Year (#05/05/1985#)      returns 1985
Year (#17/07/2005#)      returns 2005

If your field is called "myDate" for example, you can construct a query to select everything in the year 2009 like so:

SELECT * FROM myTable WHERE Year(myDate) = 2009

This function is available in both MS Access and SQL Server, too (if you're interested).

Some links for further information:

Access: Year Function
Year Function
YEAR (Transact-SQL)

CraigTP
@Bob - The original question does mention Access, which I assume is Microsoft Access. (ie. "If i have a Date/Time field in Access")!
CraigTP
This is a bad answer because first of all, it won't use indexes, and second it's not database-engine agnostic.
David-W-Fenton
@David - Database agnosticism is entirely irrelevant since the original question clearly indicates use of MS Access. While you're correct about the Year() function not using indexes, this may or may not be an issue as the question asker has not clarified the extent of the data he is querying. For example, perhaps this query with "wildcard day/month" will only ever be performed against a table with < 100 rows. Whilst using an index will help, it's definitely not required in this case. Also, since the platform is MS Access, I think we can safely assume that performance isn't a priority.
CraigTP
A: 
WHERE Year(DateColumn) = 2009
shahkalpesh
This is a bad answer because first of all, it won't use indexes, and second it's not database-engine agnostic.
David-W-Fenton
shahkalpesh
@David: Also, I would have liked, if you would have answered instead of down-voting everyone's answer. Where does the OP talk about performance?
shahkalpesh