views:

58

answers:

2

Hello everyone i want search data from invoices and client by today date I'm using DateDiff() GETDATE() functions for example two tables

1 Client

 - ID   int
 - Name Varcher

2 Invoice

 - 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

+3  A: 

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)
SQLMenace
+1: Cuz my answer, you *could park a bus* in the gap ;)
OMG Ponies
I want select query by specific day of current month and current year from date timeif the current month is 08 and current year 2010i want write any day of month
Ramy Said
change getdate() to anydate, should work just fine so if you want tomorrow you do '20100823'
SQLMenace
+1  A: 

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:

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
Mark Bannister
i know that but current month and current year fixed if i run query after month must be change date it is not i wantwhen i run query the query get month and year from date time nowso i want write day only in the query thank you
Ramy Said
@Ramy: try the new query.
Mark Bannister
@Mark thank you it is trueif you have any article about datetime, DateDiff(), Dateadd(), getdate() functions but don't send MSDN i read it i need more and examples.Thanks for help me
Ramy Said