我想从DateTime列使用SQL查询只得到时间 使用SQL Server 2005和2008 默认的输出:

AttDate                   
==
2011-02-09 13:09:00    
2011-02-09 14:10:00    

我想要这样的输出:

AttDate                Time 
==
2011-02-09 13:09:00    13:09
2011-02-09 14:10:00    14:10

当前回答

试试这个:

select  convert(nvarchar,CAST(getdate()as time),100)

其他回答

MSSQL2012或以上版本

cast(dateadd(ms,datediff(ms, [StartDateTime], [StopDateTime]),0) as Time(0))

…或…

convert(time(0),dateadd(ms,datediff(ms, [StartDateTime], [StopDateTime]),0) )

试试这个

时间日期 Select cast(getdate() as time(0)) 时间到TinyTime 选择强制转换(orig_time作为时间(0))

我经常使用这个脚本从DateTime获取时间:

SELECT CONVERT(VARCHAR(9),RIGHT(YOURCOLUMN_DATETIME,9),108) FROM YOURTABLE
select convert(char(5), tbl_CustomerBooking.CheckInTime, 108) AS [time]
from tbl_CustomerBooking

SQL Server 2012:

Select TRY_CONVERT(TIME, myDateTimeColumn) from myTable;

就我个人而言,我更喜欢TRY_CONVERT()而不是CONVERT()。主要区别:如果强制转换失败,TRY_CONVERT()将返回NULL,而CONVERT()将引发错误。