views:

30

answers:

5

Hello everyone i want search data from invoices and client by today date I'm using GETDATE()

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 i.date = getdate()

result

  nothing 

but i have some data have same date today

+1  A: 

Try following where condition

WHERE DateDiff(dd, OrderDate, getdate()) = 0
or 
WHERE Convert(varchar(20), OrderDate, 101) = Convert(varchar(20), getdate(), 101)

so your answer is

Select * 
  from client c 
        inner join invoice i on c.id = i.ClientID 
  WHERE DateDiff(dd, i.date, getdate()) = 0
Pranay Rana
not sargable, won't use index
SQLMenace
thank you June R
Ramy Said
the query is true but why to write equal 0 the Syntax is DATEDIFF (datepart ,startdate ,enddate )please answer me
Ramy Said
+1  A: 

one way

where i.date >= DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0),
and i.date < DATEADD(dd, DATEDIFF(dd, 0, GETDATE())+1 , 0)
SQLMenace
@SQLMenace - I'd deleted my answer anyway and +1ed yours as when I saw yours I realised that I'd not taken into account the possibility of future dates but it works fine. In the execution plan the predicate is `CONVERT_IMPLICIT(datetime,datediff(day,'1900-01-01 00:00:00.000',getdate()),0)` which is pretty much what yours is doing I think.
Martin Smith
A: 

Try using DATEPART instead of just getdate() (which will only match dates EXACTLY):

WHERE DATEPART(dy, GETDATE()) - DATEPART(dy, i.date) <= 1

This will give you all dates in a single day range.

Jeremy
thank you Jeremy
Ramy Said
+1  A: 

GETDATE() returns both Date and Time.

We need to floor the date to the beginning of today.

SELECT * 
FROM client c 
INNER JOIN invoice i 
   ON c.id = i.ClientID 
WHERE i.date >= CAST(FLOOR(CAST(GETDATE() AS float)) AS DATETIME)
Simmo
will also get future dates
SQLMenace
Just cast into a variable and then do a between:WHERE i.date BETWEEN @todayDate AND @todayDate+1
Simmo
thank you Simmo
Ramy Said
A: 

Select * from client c left join invoice i on c.id = i.ClientID where i.date = getdate()

Christian Cabizza