简体   繁体   English

SQL-Server Time数据类型

[英]SQL-Server Time data type

From what i've read so far, the data type time in sql server (starting with 2008) should be able to store the time in a HH:MM format. 从到目前为止的内容来看,SQL Server中的数据类型time (从2008年开始)应该能够以HH:MM格式存储时间。 Then I tried this myself with the following simple exercise: 然后,我通过以下简单的练习自己尝试了此操作:

create table #mytable (id int, lat float, lon float, trajectory_id int, theTime time(5))
insert into #mytable values 
 ('1','15.8','17.1','162','10:01'),
 ('2','11.5','59.7','162','10:02'),
 ('3','16.4','79.9','162','10:03'),
 ('4','29.5','10.3','180','11:12'),
 ('5','58.2','11.1','180','11:13'),
 ('6','54.5','14.1','180','11:14'),
 ('7','14.9','15.2','166','13:40'),
 ('8','15.0','13.1','166','13:42')

My expected result for the column theTime was: 我对theTime列的预期结果是:

theTime
10:01
10:02
10:03
11:12
11:13
11:14
13:40
13:42

What I got instead was: 相反,我得到的是:

theTime
10:01:00.00000
10:02:00.00000
10:03:00.00000
11:12:00.00000
11:13:00.00000
11:14:00.00000
13:40:00.00000
13:42:00.00000

Sure, I could select left(theTime, 5) and get the expected result. 当然,我可以选择left(theTime, 5)并获得预期的结果。 But i'm wondering how I could insert it directly in the desired format. 但我想知道如何将其直接插入所需的格式。

I want to use a built in function and not to store the numbers in an int for HH , MM , SS as one might have used before sql server 2005. 我想使用内置函数,而不要将数字存储在HHMMSSint中,因为在sql server 2005之前可能已经使用过。

Using sql server 2012. Thanks 使用SQL Server2012。谢谢

This is too long for a comment. 这个评论太长了。

You seem confused by the difference between internal formats and display formats. 您似乎对内部格式和显示格式之间的差异感到困惑。 TIME is a data type built into the language. TIME是语言中内置的数据类型。 When it prints out, it prints out with hours, minutes, and seconds. 打印时,将以小时,分钟和秒打印。 That is how the data type prints out, in the same way the SQL Server does not put commas in integers bigger than 1000 and uses an appropriate symbol for the decimal place. 这就是数据类型打印出来的方式,SQL Server不会将逗号放在大于1000的整数中,而是使用适当的符号作为小数位。 (Or for that matter prints out integers in base 10 rather than the binary format as they are actually stored.) (或者就此而言,以10为基数输出整数,而不是实际存储的二进制格式。)

When you say TIME(5) you are saying that you want at least 5 decimal places of precision for fractional seconds : HH:MM:SS.SSSSS. 当您说TIME(5)您表示要在5 秒内至少保留5位小数精度:HH:MM:SS.SSSSS。 There is no option for removing seconds altogether from the time type. 没有选项可以从time类型中完全删除秒。

If you want to print it out in a particular format, use convert() , cast() , or format() . 如果要以特定格式打印出来,请使用convert()cast()format() Happily, cast() works quite well for times . 幸运的是, cast() 有时运行良好。 The plethora of inscrutable numeric codes for date/time are more applicable to dates, than times. 日期/时间的大量难以理解的数字代码比时间更适用于日期。

When you do left(thetime, 5) then you are implicitly converting time to a string and taking the first five characters. 当您left(thetime, 5)您将隐式地将time转换为字符串并采用前五个字符。 This seems to meet your needs. 这似乎可以满足您的需求。 I would suggest that you just build this into your table: 我建议您将其构建到表中:

alter table #mytable add thetime_hhmm as (left(thetime, 5));

Then you can use thetime_hhmm to get the value as a string with the format you want. 然后,您可以使用thetime_hhmm将该值作为具有所需格式的字符串获取。

You can't control the format that a data type is stored as. 您无法控制数据类型存储的格式。 Your time data is actually being stored as a bunch of bits -- 0's and 1's -- and it's only being displayed like 10:01:00.00000 in your query analyzer. 您的时间数据实际上存储为一堆位-0和1-仅在查询分析器中显示10:01:00.00000

If you want to show it in another format, then you have to convert it to a text data type like varchar, but this is usually something your UI layer should handle, not your data layer. 如果要以其他格式显示它,则必须将其转换为文本数据类型,例如varchar,但这通常是UI层应处理的内容,而不是数据层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM