簡體   English   中英

從日期時間開始的當前月份和當前年份的特定日期

[英]Specific day of current month and current year from date time SQL Server 2005

大家好,我想在今天之前從發票和客戶中搜索數據,我使用DateDiff()GETDATE()函數作為示例,例如兩個表

1個客戶

 - ID   int
 - Name Varcher

2張發票

 - ID int
 - ClientID int
 - date  Datetime
 - Total  money

詢問

 Select * from client c 
 inner join invoice i on c.id = i.ClientID 
 WHERE DateDiff(dd, i.date, getdate()) = 0

如果當前月份是08且當前年份是2010,我想按日期月份和當前年份的特定日期選擇查詢,我想寫每月的任何一天, 謝謝大家的幫助

這是一種也可以使用索引的方法

where i.date >= DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 0)
and i.date < DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 1)

從當前月份和年份中特定日期選擇記錄的最簡單方法是聲明一個分配給指定日期,月份和年份的datetime變量,然后用該變量替換查詢中的getdate() ,如下所示:

declare @date datetime 

select @date = '10-Aug-2010'

Select * from client c 
inner join invoice i on c.id = i.ClientID 
WHERE DateDiff(dd, i.date, @date) = 0

編輯:要在當月的當月指定日期運行查詢,請嘗試以下操作:

declare @day integer

select @day = 10

Select * from client c 
inner join invoice i on c.id = i.ClientID 
WHERE DateDiff(dd, i.date, dateadd(dd,@day-datepart(dd,getdate()),getdate())) = 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM