从日期时间中提取小时 (SQL Server 2005)
我可以使用 Day(Date())
、Month(Date())
提取月份和日期。 我无法使用 HOUR(Date())
提取小时数。 我收到以下错误。
'HOUR' is not a recognized built-in function name.
如何提取小时数?
I can extract the month and day by using Day(Date())
, Month(Date())
. I can't extract hours, with HOUR(Date())
. I get the following error.
'HOUR' is not a recognized built-in function name.
How can I extract hours?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
DATEPART
文档DATEPART
documentation...您可以在任何粒度类型上使用它,即:(
注意:我喜欢日期保留字周围的 [ ] 。当然,这是为了防止您的时间戳列标记为“日期”)
... you can use it on any granularity type i.e.:
(note: I like the [ ] around the date reserved word though. Of course that's in case your column with timestamp is labeled "date")
使用日期部分。
例如:
Use datepart.
E.g.:
也试试这个:
try this one too:
DATEPART() 函数用于返回日期/时间的单个部分,例如年、月、日、小时、分钟等。
示例
The DATEPART() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.
Example
DATEPART(HOUR, [date])
返回军事时间中的小时(00 到 23)如果你想要凌晨 1 点、下午 3 点等,你需要把它列出来:
DATEPART(HOUR, [date])
returns the hour in military time ( 00 to 23 )If you want 1AM, 3PM etc, you need to case it out:
也试试这个:
Try this one too:
作品
works
有一种方法可以调用
HOUR
(不过我不建议使用它,因为有DATEPART
函数)使用 ODBC 标量函数:LiveDemo
There is a way to call
HOUR
(I would not recommend to use it though because there isDATEPART
function) using ODBC Scalar Functions:LiveDemo
要包括上午/下午 - 使用以下命令:
To include AM / PM - use the below:
您必须使用 datepart()
you must use datepart()