Search by date SQL server 2005
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
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
one way
where i.date >= DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0),
and i.date < DATEADD(dd, DATEDIFF(dd, 0, GETDATE())+1 , 0)
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)
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.
Select * from client c left join invoice i
on c.id = i.ClientID
where i.date = getdate()
精彩评论