Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where clause to filter timestamp data by using only date

Tags:

sql

sql-server

I am using a where clause to extract data but into database I have datetime stamp and I want to extract data by using only date information.

select *
from invoice
where invoice_date = '2019-06-24'

But I have into database invoice_date = 2019-06-24 04:30:00.000

like image 823
Mike Swift Avatar asked Nov 16 '25 07:11

Mike Swift


2 Answers

Cast it to date:

select *
from invoice
where CAST(invoice_date as DATE) = '2019-06-24'
like image 97
Nir Levy Avatar answered Nov 17 '25 23:11

Nir Levy


I would personally use "proper" date logic:

SELECT {Column List}
FROM dbo.invoice i
WHERE i.invoice_date >= '2019-06-24'
  AND i.invoice_date < '2019-06-25';

If you're using a parameter, then you would use DATEADD:

SELECT {Column List}
FROM dbo.invoice i
WHERE i.invoice_date >= @DateParam
  AND i.invoice_date < DATEADD(DAY, 1, @DateParam);
like image 32
Larnu Avatar answered Nov 17 '25 23:11

Larnu