Results 1 to 9 of 9

Thread: Question

  1. #1
    Join Date
    Sep 2003
    Posts
    1

    Unanswered: Question

    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?

  2. #2
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621

    Re: Question

    I like a triggers very much - you can use them in your case.
    Last edited by snail; 09-17-03 at 17:45.

  3. #3
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    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

  4. #4
    Join Date
    Oct 2001
    Location
    Naples, FL
    Posts
    273
    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.
    ---------------
    Ray Higdon MCSE, MCDBA, CCNA

  5. #5
    Join Date
    Jan 2003
    Location
    Massachusetts
    Posts
    5,800
    Provided Answers: 11
    rhigdon: Good point. Especially with this easy of a calculation.

  6. #6
    Join Date
    Oct 2001
    Location
    Naples, FL
    Posts
    273
    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...
    ---------------
    Ray Higdon MCSE, MCDBA, CCNA

  7. #7
    Join Date
    Sep 2003
    Location
    Mumbai, India
    Posts
    36

    Re: Question

    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.

  8. #8
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    If you are going to use triggers, use the DateAdd function instead. Leap years don't have 365 days.

    blindman

  9. #9
    Join Date
    Sep 2003
    Location
    Mumbai, India
    Posts
    36
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •