Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:23:32+00:00 2026-06-17T22:23:32+00:00

I am transitioning my database from storing datetimes in Pacific time into UTC, but

  • 0

I am transitioning my database from storing datetimes in Pacific time into UTC, but will not be changing all of the code at the same time.

I am trying to write a trigger that will update times entered in either column and convert them and insert them into the other column.

Ex. If I insert a Pacific time in the OriginalTime column, it should convert the time to UTC and update the NewTime column with a time in UTC. Conversely, if I insert/update the value in NewTime, it should convert the time back to Pacific and put it in OriginalTime.

Am I looping things incorrectly or something?

ALTER TRIGGER tr_Calls_CreateDate ON Calls
AFTER INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON

DECLARE
    @CallId INT,
    @CreateDate DATETIME,
    @CreateDateUTC DATETIME

--Create cursor
DECLARE CreateDate_Cursor CURSOR LOCAL FOR
    SELECT CallId, CreateDate, CreateDateUTC FROM Inserted

OPEN CreateDate_Cursor;

-- Get First Row
FETCH NEXT FROM CreateDate_Cursor INTO @CallId, @CreateDate, @CreateDateUTC;

WHILE @@FETCH_STATUS = 0
BEGIN
    IF UPDATE ([CreateDate])
    BEGIN
        UPDATE Calls
        SET CreateDateUTC = dbo.ConvertPacificTimeToUTC(@CreateDate)
        WHERE CallId = @CallId
    END

    IF UPDATE ([CreateDateUTC])
    BEGIN
        UPDATE Calls
        SET CreateDate = dbo.ConvertUTCTimeToPacific(@CreateDateUTC)
        WHERE CallId = @CallId
    END

    FETCH NEXT FROM CreateDate_Cursor INTO @CallId, @CreateDate, @CreateDateUTC;
END

-- Free Up Cursor
CLOSE CreateDate_Cursor;
DEALLOCATE CreateDate_Cursor;
END

For some reason, my current trigger is entering the converted value correctly, but the insert that caused the trigger to fire seems to be failing.

If I insert a value:

INSERT INTO Calls (CreateDate)
VALUES (GETDATE()) -- getdate currently outputs in pacific time

I get the trigger result but not the inserted value:

+-----------------+--------------------------+
|   CreateDate    |      CreateDateUTC       |
+-----------------+--------------------------+
|       NULL      | 2013-01-24 20:27:13.510  |
+-----------------+--------------------------+
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:23:33+00:00Added an answer on June 17, 2026 at 10:23 pm

    On an insert UPDATE(column) is true for all columns. That is wh your second update writes a null into the CreateDate column.

    So you need to create two triggers, One that deals with updates as in your code and one that deals with inserts. If only CreateDate is valued, CreateDateUTC is NULL and you can check for that.

    Also, you should really not use a cursor for this. Use something like this instead:

    UPDATE c SET
      CreateDate = ISNULL(CreateDate, dbo.ConvertUTCTimeToPacific(i.CreateDateUTC)),
      CreateDateUTC = ISNULL(CreateDateUTC, dbo.ConvertPacificTimeToUTC(i.CreateDate))
    FROM dbo.Calls AS c
    JOIN INSERTED AS i
    ON i.CallId = c.CallId;
    

    That is, for the insert trigger. For the UPDATE you can use something like this:

    UPDATE c SET
      CreateDate = CASE WHEN UPDATE ([CreateDateUTC]) THEN CreateDate
                      ELSE dbo.ConvertUTCTimeToPacific(i.CreateDateUTC)
                   END,
      CreateDateUTC = CASE WHEN UPDATE ([CreateDate]) THEN CreateDateUTC
                        ELSE dbo.ConvertPacificTimeToUTC(i.CreateDate)
                      END
    FROM dbo.Calls AS c
    JOIN INSERTED AS i
    ON i.CallId = c.CallId;
    

    This is all untested code, but it should work.


    EDIT: See my longer comment below for why you really should wrap this UPDATE inside of an IF statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm transitioning all of my data analysis from MATLAB to Python and I've finally
My question is super simple, but I'm transitioning from C# to C++, and I
Finally I'm transitioning to ARC. Sounds too late but all my projects have retrocompatiilty
At my company, we are transitioning (fully) from ClearCase UCM to Git. We will
I'm transitioning an application from using a normal SQLite database to one with full
I am just transitioning from Eclipse to Wing IDE for my Python code. In
From the Transitioning to ARC Release Notes Use Lifetime Qualifiers to Avoid Strong Reference
I am transitioning code that used implementations of Spring MVC's Controller to use the
i'm still transitioning from as2 to as3, i'm having trouble with parsing XML data
I am currently transitioning from VB to C# and am having some issues with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.