简体   繁体   中英

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM