Get the time of a datetime using T-SQL?

2019-01-18 17:38发布

How to get the time for a given datetime value?

I have a datetime in database like this:

2010-09-06 17:07:28.170

and want only the time portion:

17:07:28.170

Is there a function for that or something?

5条回答
在下西门庆
2楼-- · 2019-01-18 17:59

In case of SQL Server, this should work

SELECT CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond
查看更多
甜甜的少女心
3楼-- · 2019-01-18 18:01
CAST(CONVERT(CHAR(8),GETUTCDATE(),114) AS DATETIME)

IN SQL Server 2008+

CAST(GETUTCDATE() AS TIME)
查看更多
再贱就再见
4楼-- · 2019-01-18 18:07

Assuming the title of your question is correct and you want the time:

SELECT CONVERT(char,GETDATE(),14) 

Edited to include millisecond.

查看更多
beautiful°
5楼-- · 2019-01-18 18:08

Try this

SELECT CAST(DataField AS time(7)) AS 'time'

http://msdn.microsoft.com/en-us/library/bb677243.aspx

查看更多
太酷不给撩
6楼-- · 2019-01-18 18:11

Just to add that from SQL Server 2008, there is a TIME datatype so from then on you can do:

SELECT CONVERT(TIME, GETDATE())

Might be useful for those that use SQL 2008+ and find this question.

查看更多
登录 后发表回答