Hello everyone i want search data from invoices and client by today date I'm using DateDiff() GETDATE() functions for example two tables
大家好今天我要从发票和客户端搜索数据日期我使用DateDiff()获取当前日期()函数例如两个表
1 Client
1客户端
- ID int
- Name Varcher
2 Invoice
2发票
- ID int
- ClientID int
- date Datetime
- Total money
query
查询
Select * from client c
inner join invoice i on c.id = i.ClientID
WHERE DateDiff(dd, i.date, getdate()) = 0
I want select query by specific day of current month and current year from date time if the current month is 08 and current year 2010 i want write any day of month Thanks every one help me
我想按当前月的具体日期和当前年度选择查询,如果当前月是08年,当前年是2010年我想写任何一天的月份谢谢大家的帮助
2 个解决方案
#1
1
The simplest way to select records from a specific day in the current month and year is to declare a datetime variable assigned to the specified day, month and year, and replace getdate()
in your query with the variable - like so:
从当前月份和年份的特定日期选择记录的最简单方法是声明分配给指定日期、月份和年份的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
EDIT: To run a query for a specified day of the month in the current month, try the following:
编辑:要在当月某个指定的日期运行查询,请尝试以下操作:
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
#2
3
here is one way which will also be able to use an index
这里有一种方法也可以使用索引
where i.date >= DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 0)
and i.date < DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 1)
#1
1
The simplest way to select records from a specific day in the current month and year is to declare a datetime variable assigned to the specified day, month and year, and replace getdate()
in your query with the variable - like so:
从当前月份和年份的特定日期选择记录的最简单方法是声明分配给指定日期、月份和年份的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
EDIT: To run a query for a specified day of the month in the current month, try the following:
编辑:要在当月某个指定的日期运行查询,请尝试以下操作:
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
#2
3
here is one way which will also be able to use an index
这里有一种方法也可以使用索引
where i.date >= DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 0)
and i.date < DATEADD(Day, DATEDIFF(Day, 0, GETDATE()), 1)