David Jeffery
David Jeffery

Reputation: 47

How do I create a query with greater than one date and not between some other dates?

So I need to pull back data that is after 1st jan 2017 but NOT between Oct 2016 and 31st dec 2016

This is my current code:

SELECT [SequenceID]
  ,[AppointmentDate]
FROM dbo].[AmsAppointment]

where AppointmentDate >= Convert(datetime, '2017-01-01' ) and AppointmentDate <= Convert(datetime, '2016-10-01' )

I know the code is wrong so please help me.

Upvotes: 1

Views: 194

Answers (1)

Hambone
Hambone

Reputation: 16377

You can do this a number of ways, but I think a not between would work best:

SELECT [SequenceID], [AppointmentDate]
FROM [dbo].[AmsAppointment]
where
  AppointmentDate >= '2017-01-01' and
  AppointmentDate not between '2016-10-01' and '2017-12-31'

And maybe your example just that, but in your scenario, since the ranges overlap, this really translates to:

 AppointmentDate >= '2017-12-31'

Upvotes: 1

Related Questions