私は同じ問題を抱えていて、mssqlの日時範囲を満たしたいと思っていました
- 最小日時:1753-01-01 00:00:00.000(-6847804800)
- 最大日時:9999-12-31 23:59:59.997(253402300799)
これを達成するために私が見つけた唯一の解決策は、int範囲値でDATEADDを使用するようにループすることでした。
したがって、この回答に基づいて:https ://stackoverflow.com/a/2904294/687490
CREATE FUNCTION dbo.fn_ConvertToBigDateTime (@Datetime BIGINT)
RETURNS DATETIME
AS
BEGIN
DECLARE @result datetime = Convert(datetime, '01/01/1970');
DECLARE @LocalTimeOffset BIGINT
,@AdjustedLocalDatetime BIGINT
,@MinIntValue INT
,@MaxIntValue INT
,@RemainingSeconds BIGINT;
-- define int limit
SET @MinIntValue = -2147483648;
SET @MaxIntValue = 2147483647;
-- compute the datetime with the offset
SET @LocalTimeOffset = DATEDIFF(second,GETDATE(),GETUTCDATE())
SET @AdjustedLocalDatetime = @Datetime - @LocalTimeOffset
-- going to the future
WHILE(@AdjustedLocalDatetime>@MaxIntValue)
BEGIN
SET @AdjustedLocalDatetime = @AdjustedLocalDatetime - @MaxIntValue;
SELECT @result = Convert(datetime, dateadd(ss, @MaxIntValue,@result));
END
-- going back in the past
WHILE(@AdjustedLocalDatetime<@MinIntValue)
BEGIN
SET @AdjustedLocalDatetime = @AdjustedLocalDatetime - @MinIntValue;
SELECT @result = Convert(datetime, dateadd(ss, @MinIntValue,@result));
END
RETURN (SELECT DATEADD(second,@AdjustedLocalDatetime, @result))
END;
次に、次のコマンドで関数をテストできます。
select dbo.fn_ConvertToBigDateTime(-6847804800) as 'min datetime',
dbo.fn_ConvertToBigDateTime(253402300799) as 'max datetime'
それがお役に立てば幸いです。