MS SQL SERVER中时间的精度问题
来源:互联网 发布:视频特效软件 编辑:程序博客网 时间:2024/05/22 01:49
在SQL SERVER中时间的精度为1/300秒,换句话说在SQL SERVER中1代表1/300S,所以1S在SQL SERVER中存储时对应的十进制数据是300,十六进制为12C。300是这样的到的:1除以1/300。 那么在SQL SERVER中的其他的ms如何表示呢? 在下面的例子中,使用一个循环在表中插入1000行,每行分别增加1ms,但是在SQL SERVER中表示并不是按照0.001,0.002,0.003等来表示的,而是进行了相应的处理。
declare @tb table(id int identity(0,1),date datetime)
declare @count int,@i floatset @count=0
while(@count<=1000) begin
insert into @tb values(dateadd(ms,@count,'00:00:00.000'))
set @count=@count+1
end
select id,date ,cast(date as binary(8)) as [binary] ,cast(cast(date as binary(8)) as int) as [int]from @tb
结果为:id date binary int
0 1900-01-01 00:00:00.000 0x0000000000000000 0
1 1900-01-01 00:00:00.000 0x0000000000000000 0
2 1900-01-01 00:00:00.003 0x0000000000000001 1
3 1900-01-01 00:00:00.003 0x0000000000000001 1
4 1900-01-01 00:00:00.003 0x0000000000000001 1
5 1900-01-01 00:00:00.007 0x0000000000000002 2
6 1900-01-01 00:00:00.007 0x0000000000000002 2
7 1900-01-01 00:00:00.007 0x0000000000000002 2
8 1900-01-01 00:00:00.007 0x0000000000000002 2
9 1900-01-01 00:00:00.010 0x0000000000000003 3
10 1900-01-01 00:00:00.010 0x0000000000000003 3
999 1900-01-01 00:00:01.000 0x000000000000012C 300
1000 1900-01-01 00:00:01.000 0x000000000000012C 300
对于0.000来说对应的二进制数字为0,那么对应的数字为0 ,
0.001来说对应的二进制数字如何得到呢?1/1000除以1/300得到0.3四舍五入为0,那么对应的数字为0
0.002来说对应的二进制数字如何得到呢?2/1000除以1/300得到0.6四舍五入为1,那么对应的数字为1
0.003来说对应的二进制数字如何得到呢?3/1000除以1/300得到0.9四舍五入为1,那么对应的数字为1
0.004来说对应的二进制数字如何得到呢?4/1000除以1/300得到1.2四舍五入为1,那么对应的数字为1
0.005来说对应的二进制数字如何得到呢?5/1000除以1/300得到1.5四舍五入为2,那么对应的数字为2
0.006来说对应的二进制数字如何得到呢?6/1000除以1/300得到1.8四舍五入为2,那么对应的数字为2
0.007来说对应的二进制数字如何得到呢?7/1000除以1/300得到2.1四舍五入为2,那么对应的数字为2
0.008来说对应的二进制数字如何得到呢?8/1000除以1/300得到2.4四舍五入为2,那么对应的数字为2
0.009来说对应的二进制数字如何得到呢?9/1000除以1/300得到2.7四舍五入为3,那么对应的数字为3
0.010来说对应的二进制数字如何得到呢?10/1000除以1/300得到3四舍五入为3,那么对应的数字为3
0.011来说对应的二进制数字如何得到呢?11/1000除以1/300得到3.3四舍五入为3,那么对应的数字为3
那么
DECLARE @t TABLE(date char(21))
INSERT @t SELECT '1900-1-1 00:00:00.000'
INSERT @t SELECT '1900-1-1 00:00:00.001'
INSERT @t SELECT '1900-1-1 00:00:00.002'
INSERT @t SELECT '1900-1-1 00:00:00.003'
INSERT @t SELECT '1900-1-1 00:00:00.004'
INSERT @t SELECT '1900-1-1 00:00:00.005'
INSERT @t SELECT '1900-1-1 00:00:00.006'
INSERT @t SELECT '1900-1-1 00:00:00.007'
INSERT @t SELECT '1900-1-1 00:00:00.008'
INSERT @t SELECT '1900-1-1 00:00:00.009'
SELECT date,转换后的日期=CAST(date as datetime) FROM @t结果为: date 转换后的日期
1900-1-1 00:00:00.000 1900-01-01 00:00:00.000
1900-1-1 00:00:00.001 1900-01-01 00:00:00.000
1900-1-1 00:00:00.002 1900-01-01 00:00:00.003
1900-1-1 00:00:00.003 1900-01-01 00:00:00.003
1900-1-1 00:00:00.004 1900-01-01 00:00:00.003
1900-1-1 00:00:00.005 1900-01-01 00:00:00.007
1900-1-1 00:00:00.006 1900-01-01 00:00:00.007
1900-1-1 00:00:00.007 1900-01-01 00:00:00.007
1900-1-1 00:00:00.008 1900-01-01 00:00:00.007
1900-1-1 00:00:00.009 1900-01-01 00:00:00.010
了解了上面的日期的表示方式后,0.000与0.001为什么表示成0.000就好理解了。
0.000与0.001使用0来表示,那么使用0乘以1/300后为0,所以转换后的日期为1900-01-01 00:00:00.000
0.002、0.003、0.004使用1来表示,那么使用1乘以1/300后为0.0033,进行四舍五入,所以转换后的日期为1900-01-01 00:00:00.003
0.005、0.006、0.007、0.008使用2来表示,那么使用2乘以1/300后为0.0066,进行四舍五入,所以转换后的日期为1900-01-01 00:00:00.007
0.009使用3表示,那么使用3乘以1/300后为0.010,进行四舍五入,所以转换后的日期为1900-01-01 00:00:00.010
- MS SQL SERVER中时间的精度问题
- Sql时间比较的问题MS-SQL Server
- MS SQL Server 中Convert转换时间的用法
- 在Delphi中使用MS SQL Server ,MS Access遇到的问题
- 解决MS SQL Server 死锁的问题
- MS SQL SERVER表分区的问题
- MS SQL Server利用日志备份恢复数据到时间点的问题
- linux中时间精度的获取问题
- MS sql server中时间函数用法详解 DATEADD DATEDIFF
- sql中查询数据精度的问题
- MS-SQL Server当前系统时间的日期部分
- MS SQL Server 数据库日期时间函数的应用
- MS-SQL Server获取当前系统的日期和时间
- MS SQL Server登陆问题
- MS SQL Server 连接问题
- MS SQL Server中dbo
- 关于在java中连接MS SQL Server 2005 JDBC的问题及解决办法
- 解决PB9.0版本 db profile中 MSS MS SQL Server选项不见的问题?
- 第二人生的源码分析(五十)纹理图片的格式之引用类
- 有借(debit)必有贷(credit),借贷必相等(摘自网络)
- 计算机登陆域速度慢的分析与解决
- 照明设计原理
- 电脑蓝屏后该做的几件事情
- MS SQL SERVER中时间的精度问题
- c++中的字符数组
- Set irregular shape for windows form
- 用位运算获得集合的所有子集
- Widows 文件操作和内存映射
- swt-designer配置开发环境
- VC++快捷键大全
- 使用mysqlbinlog查看二进制日志的问题
- Oracle 精妙SQL语句集锦