Home > Software engineering >  Exclude row with date having day as Sunday & cell value is zero or null (in SQL Server)
Exclude row with date having day as Sunday & cell value is zero or null (in SQL Server)

Time:07-31

Need help with a T-SQL query.

How to exclude row with date having day as Sunday & cell value is zero or null in SQL Server.

Excluding Sunday is one condition with solutions like this link

Now I need to apply a second condition - only to exclude Sundays if cell value is zero or null.

CodePudding user response:

Your question is extremely vague and you should definitely consider reading these pages for the future:

How do I ask a good question?

How to create a Minimal, Reproducible Example

However, here is an example which I think answers your question:

declare @sampleData table
(
    DateValue date,
    cell int
)

insert into @sampleData values
('2021-11-03',1),
('2021-11-04',2),
('2021-11-05',0),
('2021-11-06',4),
('2021-11-07',null),
('2021-11-08',0),
('2021-11-09',2),
('2021-11-10',2),
('2021-11-13',null),
('2021-11-13',0)

select *
from @sampleData
where datepart(dw,DateValue) <> 7
or isnull(cell,0) <> 0

This just excludes and Sundays where the "cell" column contains a zero or NULL value. As you can see, the Sunday 6th November is included as the cell value is 4 and the entries for 7th/8th November are included because while they have cell values of 0 and NULL, they are not Sundays. Only the entries for Sunday 13th November are excluded, because they are Sundays AND have a cell value of either 0 or NULL.

  • Related