-3

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sahsha
  • 37
  • 1
  • 5
  • 2
    include an `AND` in your `WHERE`? Excluding the Sundays was the "hard part". – Thom A Oct 29 '21 at 12:15
  • 1
    Just as a side note: SQL Server 2008 and 2008 R2 are **completely unsupported** (out of even extended support) by now - https://www.red-gate.com/simple-talk/sql/database-administration/the-end-of-sql-server-2008-and-2008-r2-extended-support/ - time to upgrade! – marc_s Oct 29 '21 at 12:49
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 30 '21 at 20:48

1 Answers1

0

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.

3N1GM4
  • 3,372
  • 3
  • 19
  • 40