sql server 2008 - How to set flag variable if year is changed in comparing two dates -


suppose example have start date 31-12-2013 & end date 6-01-2014. want set flag =1 in sql server database table if start date in previous year & end date in next year. how this?

you can using computed column. example, have created table following script:

create table tblcomputedsample     (start_date datetime,     end_date datetime,     flag (case when year(end_date) - year(start_date) = 1 1 else 0 end)     ) 

note column flag, automatically value based on values in start_date , end_date fields.

insert values , check data.

i inserted sample data:

insert tblcomputedsample     values('1-mar-2014','10-nov-2014'),('1-mar-2013','10-nov-2014'),               ('1-mar-2014','10-nov-2015'),('10-may-2014','20-dec-2014') 

and checked value of flag.


Comments

Popular posts from this blog

C# random value from dictionary and tuple -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -