Let’s begin with the purpose of the project :
-> I have a table with a column Date_Election_President the type is Datetime…
Begin with the day of his election, the President cannot reach more than two years
And in the same table I have an another column President_Eligible of type varchar(3) which takes only ‘Yes’ or ‘No’
- Yes – if the President did not complete the two years yet
- No – if the President did complete the two years
I’m asking you if there is any way in SQL Server to automatically change the President_Eligible column from ‘Yes’ to ‘No’ if the President did complete the two years?
This is the table
create table GIAC
(
Id_GIAC smallint primary key identity(1,1),
Nom varchar(50) not null,
Adress varchar(50) not null,
President varchar(50) not null,
President_Eligible varchar(20),
Date_Election_President datetime,
)
Of course, if you do want it “in” your table, you can use a computed column:
And then you don’t have to remember to include this logic in every query – putting it in every query is a risk if the rule will ever change.
I’ve also changed to counting the number of days since election, rather than doing
DATEDIFF(year,..., sinceDATEDIFFcounts the number of transitions of the identified date portion between the two dates. For instance, theDATEDIFF(year,...between 31st December 2011 and 1st January 2012 is 1.