usr..
7
我单独使用T-SQL无法找到任何方法.我用SQL CLR解决了它:
public static class DateTimeFunctions
{
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static DateTime? ToLocalTime(DateTime? dateTime)
{
if (dateTime == null) return null;
return dateTime.Value.ToLocalTime();
}
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static DateTime? ToUniversalTime(DateTime? dateTime)
{
if (dateTime == null) return null;
return dateTime.Value.ToUniversalTime();
}
}
以下注册脚本:
CREATE FUNCTION ToLocalTime(@dateTime DATETIME2) RETURNS DATETIME2 AS EXTERNAL NAME AssemblyName.[AssemblyName.DateTimeFunctions].ToLocalTime;
GO
CREATE FUNCTION ToUniversalTime(@dateTime DATETIME2) RETURNS DATETIME2 AS EXTERNAL NAME AssemblyName.[AssemblyName.DateTimeFunctions].ToUniversalTime;
被迫转换到UTC时间的努力是一种耻辱.
请注意,这些函数将本地时间解释为服务器的本地时间.建议将客户端和服务器设置为相同的时区,以避免混淆.