0

I have one table with the following structure and some values :

PK_ID      Webinar_Name              Start_Date                         Is_Active

1                          w1                          3/1/2016                          True

2                          w2                          1/7/2016                          True

3                          w3                          4/9/2016                          True

Now i want the Is_Active column value to be updated dynamically (i.e : NOT after update,insert and delete) based on the date.. if its matches the current date so the Is_Active column value will set to be false.

I've tried to use triggers but it should be after some action like insert and update which is conflicts with my requirements.

I appreciate any help . thanks

2 Answers 2

3

It looks like you should be able to use a computed column for this, though I've never tried it.

https://msdn.microsoft.com/en-GB/library/ms188300.aspx

I guess it'd look something like this:

ALTER TABLE webinars ADD ActiveFlag AS
    CASE WHEN Start_Date = CAST(GetDate() AS date) THEN 'True'
    ELSE 'False' END
Sign up to request clarification or add additional context in comments.

Comments

2

Use a computed column:

alter table t add Is_Active as (case when cast(start_date as date) = cast(getdate() as date) then 0 else 1 end)

Of course, you might need to remove the column first.

Note: this uses 1 for true and 0 for false. These constants are not defined in SQL Server. You can use whatever values you like.

1 Comment

You'd probably also want to remove the time part from the date returned by GetDate(), otherwise it'll only match at precisely midnight on the start date

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.