If you need to do this a lot, and if you are on SQL Server 2005 or newer, you can also do this:
add three calculated columns for the day, month, year of your date and save those
ALTER TABLE dbo.YourTable ADD DayPortion AS DAY(YourDateTimeColumn) PERSISTED
enter the index in three columns:
CREATE NONCLUSTERED INDEX IX_DatePortions ON dbo.tblMessages(YearPortion, MonthPortion, DayPortion)
Now you can easily and quickly search in those days, months, a year and an index, your search will be very quick and fast.
SELECT (list of columns) FROM dbo.tblMessages WHERE YearPortion = 2011 AND MonthPortion = 4 AND DayPortion = 17
With this setting — three calculated, saved columns — you can now simply insert new rows into the table — these three columns will be calculated automatically.
Because they are stored and indexed, you can search these columns easily and very efficiently.
And with such flexibility, you can also easily find, for example. all rows for a given month or year are very beautiful.
source share