Will F
Will F

Reputation: 437

Calculated field to capture +/- 28 days from date

I need to create a calculated field that will give me 'yes' or 'no' if a date field is within +/- 28 days of another date field. So for example if a.date = 1/1/15 and b.date is 30/3/15 the calculated field will say 'no'; if the b.date was 10/1/15 it would say yes. Similarly, if the b.date was 1/11/14 it would say 'no'; if it was 10/12/14 it would say 'yes'.

I appreciate I will need to write an IIF statement, probably nested, but am struggling with this, so any suggestions would be very welcome.

Thanks

Upvotes: 0

Views: 33

Answers (1)

Anton Gogolev
Anton Gogolev

Reputation: 115799

It's pretty much this (case statement to rescue):

create table [Sample] ( 
  ID int identity,
  DateA datetime,
  DateB datetime,
  Result as case
    when DateA between dateadd(dd, -28, DateB) and dateadd(dd, 28, DateB) then 'yes'
    else 'no'
  end
)

Upvotes: 2

Related Questions