UPDATE Transactions
SET TimeTaken
    = (CASE WHEN NewValue~E'^-?\\d+$' THEN NewValue::integer ELSE 0 END)
    - COALESCE(OldValue::integer, 0)
WHERE ObjectType = 'RT::Ticket' AND Type = 'Set' AND Field = 'TimeWorked';