문제

I'm using Microsoft SQL Server 2000 (version 8); I would like to query and return all records for a given month.

I would provide the month and year and would get every record with a date falling within that period.

I'm hoping there is a way that is more elegant than what I've seen on google with looping through days or determining last day.

도움이 되었습니까?

해결책

This is the simplest way but not performance efficient. as this query will not make use of any indexes defined on the DateColumn.

SELECT *
FROM Table_Name
WHERE MONTH(DateColumn) = 12    --<-- Month December
AND   YEAR(DateColumn)  = 2013  --<-- Year 2013

A better approach will be something like ...

For example if you want all the records in JUNE 2013 you could do something like .....

SELECT *
FROM DateColumn >= '20130601'
AND  DateColumn <= '20130630'

다른 팁

From within Microsoft Query I use: docdate

=#1/09/2022# And <=#30/09/2022#

I'm still trying to find a more elegant way just to refer to all of the month before current.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top