当前位置:   article > 正文

sqlserver获取datetime毫秒转数字_sqlserver 时间转数字

sqlserver 时间转数字

 sqlserver  日期时间  datetime 转成纯数字,

有这样的需求的应该比较多

正常思维

用convert转换成yyyy-mm-dd hh:mm:ss.nnn

select  convert(varchar(24),getdate(),121)

然后把字符全部replace掉就ok了

我下面的函数是用数学计算,合计出数字类型numeric然后返回

秒是14位,毫秒是17位,int存不下 只能用 numeric

首先计算出年月日串 : @date

@date= 10000*年+100*月+日

时间串:@time

@time=10000000 * 小时 + 100000*分钟 + 1000*秒 + 毫秒

然后合计起来  :@date*1000000000 + @time

具体代码:

  1. if exists(select * from sysobjects where name = 'f_get_datetime_ms' and type = 'FN')
  2. drop function f_get_datetime_ms;
  3. go
  4. create function f_get_datetime_ms()
  5. returns numeric
  6. as
  7. begin
  8. declare @date numeric;
  9. declare @time numeric;
  10. select @date = 10000*datepart(yy, getdate()
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/289717
推荐阅读
相关标签
  

闽ICP备14008679号