SQL query that allows a template month / day, but a specific year

If I have a Date / Time field in Access and I need to find all the records that are in a specific year ... let's say 2009. If my fields are configured as MM / DD / YYYY, how would I do the query, essentially / / 2009

update Thanks for helping everyone, but I think the problem is that I am kind of dynamically building my SQL statement in ASP.

"SELECT * FROM database WHERE (datecolumn LIKE " & string & ")" 

      

where the string ends up returning the string I built "DD / MM / YYYY". If the user doesn't enter anything in any of the three fields, I will need to make a specific query if that part is a wildcard ... as if they didn't fit in a day, I would really need

"SELECT * FROM database WHERE (datecolumn LIKE "*/MM/YY")"

      

.... which doesn't work in Microsoft Access

+2


source to share


4 answers


What about



WHERE MyDate >= '1/1/2009' AND MyDate < '1/1/2010'

      

+5


source


You can use the function Year()

Some examples:

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

      

If your field is named "myDate", for example, you can build a query to select everything in 2009 like this:



SELECT * FROM myTable WHERE Year(myDate) = 2009

      

This feature is also available in both MS Access and SQL Server (if you're interested).

Some links for more information:

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

+3


source


WHERE Year(DateColumn) = 2009

      

0


source


OTTOMH

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

      

-1


source







All Articles