Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query to fetch records for alternate days based on a column

Tags:

date

sql

I have a table altstore with a column checkindate which keeps the date. My requirement is to fetch records of alternate days for a month for example of day 1st of the month, then 3rd and so on. Please help me with the sql query which I can use. Thanks.

like image 739
Sid Avatar asked Dec 04 '25 13:12

Sid


2 Answers

Try using:

select * from altstore
where DATEDIFF (day,startingdate,enddate) % 2 is 1

here startingdate is first date of the month and enddate is value in column checkindate.

like image 128
Shraddha Avatar answered Dec 07 '25 01:12

Shraddha


Use datepart and modulo:

select
    *
from
    table
where
    datepart(dd, checkindate) % 2 = 1
    and checkindate between '2011-12-01' and '2011-12-31'
like image 42
Eric Avatar answered Dec 07 '25 01:12

Eric



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!