简体   繁体   English

将所有日期时间列批量更新为 UTC 的最佳方法是什么

[英]What would be the best way to bulk update All Datetime columns to UTC

I am looking for suggestion I have a few databases and deployed in Germany & UK Datacenters so all DateTime columns contain stores Datetime values according to their region.我正在寻找建议,我有一些数据库并部署在德国和英国数据中心,因此所有 DateTime 列都包含根据其区域存储的 Datetime 值。

Now my organization want to deploy Germany Database to UK Server to save licensing cost and I need to figure out the way that all applications keep on working as they are now.现在我的组织希望将德国数据库部署到英国服务器以节省许可成本,我需要弄清楚所有应用程序如何继续像现在一样工作。

The biggest issue is the DateTime columns, I need to find the optimum solution to change all DateTime columns data to UTC DateTime.最大的问题是 DateTime 列,我需要找到将所有 DateTime 列数据更改为 UTC DateTime 的最佳解决方案。

I believe my stored procedure will keep working fine as I am moving databases to UK region.我相信当我将数据库移动到英国地区时,我的存储过程将继续正常工作。

I need to update my C# code to display region specific date time when displaying data我需要更新我的 C# 代码以在显示数据时显示区域特定日期时间

Solution 1解决方案 1

You can use German locales on your British servers.您可以在您的英国服务器上使用德语语言环境。 If that's a problem with your app, then you can adjust the values in the app.如果这是您的应用程序的问题,那么您可以调整应用程序中的值。

Solution 2解决方案 2

You can find all DateTime columns of all tables in your database您可以找到数据库中所有表的所有DateTime

select schema_name(t.schema_id) + '.' + t.name as [table],
       c.column_id,
       c.name as column_name,
       type_name(user_type_id) as data_type,
       scale as second_scale
from sys.columns c
join sys.tables t
     on t.object_id = c.object_id
where type_name(user_type_id) in ('date', 'datetimeoffset', 
      'datetime2', 'smalldatetime', 'datetime', 'time')
order by [table],
         c.column_id;

(you can of course adjust the types if needed) and you can even generate a script for each column: (如果需要,您当然可以调整类型),您甚至可以为每一列生成一个脚本:

select 'update ' + schema_name(t.schema_id) + '.' + t.name + ' set ' + column_name + ' = dateadd(hour, -1, ' + column_name + ')'
from sys.columns c
join sys.tables t
     on t.object_id = c.object_id
where type_name(user_type_id) in ('date', 'datetimeoffset', 
      'datetime2', 'smalldatetime', 'datetime', 'time')
order by [table],
         c.column_id;

and then run the scripts generated this way.然后运行以这种方式生成的脚本。 Note that this is untested, so you should test this very well before you apply this on your actual database.请注意,这是未经测试的,因此在将其应用于实际数据库之前,您应该对其进行很好的测试。

Solution 3解决方案 3

Always subtract an hour from dates earlier than a given date.总是从早于给定日期的日期中减去一个小时。 This is an admittedly messy solution and it should only be done if all else fails.这是一个公认的混乱解决方案,只有在所有其他方法都失败时才应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM