从datetime获取时间? [英] Getting the hour from a datetime?

查看:137
本文介绍了从datetime获取时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 datetime 中选择小时?

我正在寻找相当于Oracle的 TO_CHAR(d,'HH24'),将返回 17 2010-06- 23 17:22:31

I'm looking for the equivalent of Oracle's TO_CHAR( d, 'HH24' ), which would return 17 for 2010-06-23 17:22:31.

我试图找出最好的时间最好的答案/任务)在StackOverflow上使用 SQL 相关问题新的rel =nofollow noreferrer>数据浏览器。我得到它的工作,但我确定有一个更优雅的版本。

I tried to find out about the best time (best answer/questin-ration) to ask an SQL-related question on StackOverflow, using Data Explorer. I got it to work, but I'm sure there is a more elegant version.

Select
  Left( Convert(VARCHAR(10), creationDate, 8 ), 2 ) As hour,
  Convert(Float, Count(*)) / Count(Distinct p.parentId) As ratio
From posts p
Join postTags pt On ( pt.postId = p.parentId )
Join tags t On ( t.id = pt.tagId )
Where p.postTypeId = 2 And t.tagName = '##TagName##'
Group By Left( Convert(VARCHAR(10), creationDate, 8 ), 2 )
Order BY hour






BTW,最好的时间似乎在 15:00 和<$ c之间$ c> 16:00 - 猜我问太晚了:)


BTW, the best time seems to be between 15:00 and 16:00 - guess I'm asking too late :)

alt text http://img97.imageshack.us/img97/5421/besttimetoasksqlquestio.png

推荐答案

使用 DATEPART

select DATEPART(hour, DateTimeField) as Hour from Table

这篇关于从datetime获取时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆