SQL Server – How to Get Data from 1st-15th and 16th-Last Day of Previous Month

sql server

I have a table that have these values:

From data   | Todate     | is_Complete

2018-11-01 |  2018-11-15   |    True
2018-11-16 | 2018-11-30   |    True

We are executing data every 1st day of the month. So assuming that It's already 1st day of december. I need to check if is_Complete column is True from first day of the previous month and 16th up to last day of the previous month then process my queries but if one of those is False I will not process it.

I am not sure if CAST will work here.

IF both is_Complete == True, then

process queries ELSE 'N/A'

Best Answer

declare @AA_WC_TEST_PAYRUN table(from_date date, to_date date, is_Complete varchar(10));
insert into @AA_WC_TEST_PAYRUN 
values
('2018-10-01', '2018-10-15', 'True'),
('2018-10-16', '2018-10-30', 'True');

if (select is_Complete
    from @AA_WC_TEST_PAYRUN
    where from_date = (select dateadd(month, datediff(month, 0, getdate())-1, 0))) = 'True'
and
    (select is_Complete
    from @AA_WC_TEST_PAYRUN
    where from_date = (select dateadd(day, 15, dateadd(month, datediff(month, 0, getdate())-1, 0)))) = 'True'
begin
   print 'both true';
   -- other statements
end;