Changing node values directly to database exhibits erratic behaviour
The safest way would be adding field data and saving it with node_save. If you write it to the database, you have to also duplicate the record to the revision table of that field and keep track of vids. The API will just do this for you. You do not (should not) write field data via db_insert or other functions like it. Doing so may irreparably harm your database.

To apply the data retroactively, I would advise that you create a batch op via Batch API. Query up all nodes, create batch operations, and in your batch callback, process the node and save it with node_save.

There is a docs page on getting started with the Batch API.

Here is a blog post that demonstrates a basic batch job that is close to what you'd want to start out with. You'd want to populate the $operations array with node ids, and every time the batch callback is called, the node id is passed. Then you load the node with it, set the field, then save the node.

edit: tutorial author also provides a Drupal 8 version.