1

SQLで特定の月の初日から最終日までのデータを取得する方法は?

以下はSQLクエリです助けてください

DECLARE @mydate DATETIME
SELECT @mydate = GETDATE()
select TransactionCode from ReceiptsPayments 
where ReferenceDate >= (SELECT CONVERT(VARCHAR(25),
                                       DATEADD(dd,-(DAY(@mydate)-1),@mydate),
                                       101) AS Date_Value)
  AND ReferenceDate <= (SELECT CONVERT(VARCHAR(25),
                                       DATEADD(dd,
                                               -(DAY(DATEADD(mm,1,@mydate))),
                                               DATEADD(mm,1,@mydate)),
                                       101))
GO
4

4 に答える 4

4

これを試してみてください -

DECLARE @mydate DATETIME
SELECT @mydate = GETDATE()

SELECT TransactionCode 
FROM dbo.ReceiptsPayments 
WHERE MONTH(ReferenceDate) = MONTH(@mydate) 
    AND YEAR(ReferenceDate) = YEAR(@mydate)
于 2013-06-11T06:38:10.847 に答える
3
select TransactionCode 
from ReceiptsPayments 
where 
month(ReferenceDate) = month(@mydate) 
and 
year(ReferenceDate) = year(@mydate) ;
于 2013-06-11T06:37:19.327 に答える
0
DECLARE @mydate DATETIME 
SELECT @mydate = GETDATE() 

select TransactionCode 
  from ReceiptsPayments 
 where ReferenceDate >= DATEADD(month, DATEDIFF(month, 0, @mydate), 0)
   and ReferenceDate <= DATEADD(day, -1, DATEADD(month, DATEDIFF(month, 0, @mydate) + 1, 0))
于 2013-06-11T06:32:20.380 に答える