I have a fromdate column and a todate column in a table. I want the todate column to be one year later then the fromdate. I have the dates for the fromdate column, is there an easy way to set the values in the todate column?I like a triggers very much - you can use them in your case.|||If you are not planning on updating the todate at all, you can use a computed column:
create table test1
(fromdate datetime not null,
todate as dateadd (yy, 1, fromdate))
insert into test1 (fromdate)
values (getdate())
select *
from test1|||If you are not planning on ever updating the todate (or really even if you were), I don't know why you would ever even store it. If you needed it you could always just calculate it.|||rhigdon: Good point. Especially with this easy of a calculation.|||THere are times where it saves time to store calcs, like on some databases I have stored the business days difference between two dates within the table to save time, but for something as simple as this...|||Originally posted by adama777
I have a fromdate column and a todate column in a table. I want the todate column to be one year later then the fromdate. I have the dates for the fromdate column, is there an easy way to set the values in the todate column?
You can use the following statement:
update Table_X
set todate = fromdate + 365
--Suresh.|||If you are going to use triggers, use the DateAdd function instead. Leap years don't have 365 days.
blindman|||Yes. Yes. I forgot about the leap years. You can simply use:
You can use the following statement:
update Table_X
set todate = dateadd(yyyy, 1, fromdate)
--Suresh.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment