当前位置: 首页 > news >正文

MSSQL:自定义时间转时间戳函数 F_TOUnixTimestamp()

SQL SERVER 2008 版本

做两日期减运算,

使用DATEDIFF( second, '1970-01-01', getdate() )  返回值: 最大2的31次的整数,

当日期getdate()>= 2037-01-19 时函数溢出报错,在指定平台环境下如何 解决日期getdate()>= 2037-01-19 返回时间戳?

在SQL SERVER 2016以及更高版本里有DATEDIFF_BIG() 函数, 此函数可以解决。但在 SQL SERVER 2008 版本里 没有 DATEDIFF_BIG() 函数,

解决 超过68年的BUG,在自定义函数里通过WHILE循环多次使用 DATEDIFF() 处理,具体函数代码:

IF OBJECT_ID('F_toUnixTimestamp')>0 
drop FUNCTION F_toUnixTimestamp;
GO 
CREATE FUNCTION F_toUnixTimestamp(@DATETIME DATETIME ) 
RETURNS BIGINT AS BEGIN
-- 参数: @datetime :是北京时间的日期时间格式
-- 输出结果将是一个长整型数字,表示从1970年1月1日到指定日期的秒数(UTC时间)。
-- 返回: UTC 时间戳(10位整数型)declare @result bigint, @result1 bigint,  @result2  bigint;declare @initial_datetime datetime2  ;declare @datetime2 datetime2 , @startdatetime datetime2 ;declare @mod bigint , @year bigint, @initial_year bigint ;declare @i bigint , @times bigint, @max bigint; -- declare @datetime datetime;-- set @datetime = CONVERT(DATETIME2,'2108-01-01 00:00:00.0000' ,121);-- set @datetime2 = CONVERT(DATETIME2,'2038-01-01 00:00:00.0000' ,121);set @initial_datetime = CONVERT(DATETIME2,'1970-01-01 00:00:00.0000' ,121);-- select @RESULT = DATEDIFF( second, @initial_datetime , @DATETIME) - 28800 ; set @year = DATEPART(year, @datetime ) ;set @i = 1 ;set @result2 = 0;set @result1 = 0;set @result = 0; select  @times = (@year - 1970 )/ 68 , @max = ceiling((@year - 1970 )/ 68.0 );-- print ' @times =' + str(@times ) + ' @max = '+str(@max) while @i<@max  begin-- 4354790400 if @i<=@timesbeginset @datetime2 =  DATEADD(year,68* (@i+1) , @initial_datetime  )  ;set @result1 = DATEDIFF( second,  DATEADD(year,68* (@i) , @initial_datetime  ) , @DATETIME2) ;--  print '@result1_i= ' +str(@result1);set @result2 = @result2 + @result1 ;end;if @i=@times and @i<@max beginset @datetime2 =  @datetime ; --DATEADD(year,68* (@i) , @initial_datetime  )  ;set @result1 = DATEDIFF( second,  DATEADD(year,68* (@i) , @initial_datetime  ) , @DATETIME2) ;--  print '@result1_2= ' +str(@result1);set @result2 = @result2 + @result1 ;--  print '@result2_2= ' +str(@result2);end;		 set @i = @i+1;-- SET @result2 = @result2 + @result1 ;-- print ' @times =' + str(@times ) + ' ; @max = '+str(@max)  + ' ;@i = ' + cast(@i as nvarchar(10))end; if  @times = 0 select @RESULT = DATEDIFF( second, @initial_datetime , @DATETIME) - 28800 ; elseset @result = @result2  - 28800 ; return @result ;--  print '@result= ' + cast ( @result as nvarchar(200));
END; SELECT [dbo].[F_toUnixTimestamp64]( '2258-01-01 00:20:00')  AS toUnixTimestamp64 ,[dbo].[F_toUnixTimestamp]( '2258-01-01 00:20:00')  AS toUnixTimestamp

  

 

http://www.gsyq.cn/news/42371.html

相关文章:

  • dotnet 读 WPF 源代码 学习使用 Microsoft.DotNet.Arcade.Sdk 处理代码里的多语言
  • 【GitHub每日速递 20251107】DeepCode来袭!多代理系统革新代码生成,多项指标吊打同行!
  • 2025年11月北京离婚离婚律所推荐评测:多维指标客观评估报告
  • 2025年11月岗亭定制厂家推荐评价:从资质认证到案例实践的全景分析
  • 2025年11月AI搜索优化推荐榜单:五家优质服务商全面对比
  • 2025年6月ai搜索排名优化推荐:实测评价榜揭晓五强服务商
  • 日程记录 20251106
  • claude code的问题
  • 如何冻结llava的参数,在训练时不动
  • 解决CentOS 7中NAT模式无法连接网络
  • Linux 音频管道测试
  • 2025 年 11 月冷库板/聚氨酯冷库板/机制板, 冷库门, 冷库工程/冷库集成厂家推荐排行榜:专业制造与高效保温系统解决方案
  • Codeforces Global Round 30 (Div. 1 + Div. 2)
  • 【esp32 学习笔记】采用 millis() 函数的非阻塞循环的写法
  • 【ESP32 进阶】非阻塞循环的写法
  • 2025年11月北京房产纠纷律师排名分析:客观评价与实务参考
  • 2025年11月学生平板品牌评测榜:从双师1对1到全科AI精准学横向对比
  • 2025年11月智能学习机品牌对比榜:新课标同步与护眼大屏机型排名
  • 2025年11月学习机品牌推荐榜:AI精准学机型口碑对比评测
  • 2025年11月干皮精华产品精选榜:五款干敏肌适用精华对比
  • 2025年11月学习机品牌推荐:新课标同步榜评测盘点
  • 2025年11月黄褐斑改善产品评价榜:五款临床级单品数据解析
  • 2025年11月适合小学生的学习机推荐榜:五强参数与体验全解析
  • 2025年11月婚礼前美白产品推荐榜:准新娘淡斑评价合集
  • 2025年11月色斑淡化产品推荐榜:五款精华临床数据对比与评价
  • 2025年11月色斑淡化产品推荐榜:临床数据对比五款淡斑精华表现
  • 2025强网拟态 -- CTF方向 -- Crypto
  • 2025.11.7 月考游记 - Amy
  • 每日反思(2025_11_05)
  • CCPC济南站F题目的思考