Get the time of a datetime using T-SQL

6rqinv9w  于 2023-02-21  发布在  其他
关注(0)|答案(6)|浏览(148)

How can I 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?

zf9nrax1

zf9nrax11#

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.

cl25kdpy

cl25kdpy2#

In case of SQL Server, this should work

SELECT CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond
iqjalb3h

iqjalb3h3#

Try this:

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

See time (Transact-SQL).

yizd12fk

yizd12fk4#

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

SELECT CONVERT(char,GETDATE(),14)
ugmeyewa

ugmeyewa5#

CAST(CONVERT(CHAR(8),GETUTCDATE(),114) AS DATETIME)

In SQL Server 2008 and later

CAST(GETUTCDATE() AS TIME)
vmdwslir

vmdwslir6#

You can try the following code to get time as HH:MM format:

SELECT CONVERT(VARCHAR(5),getdate(),108)

相关问题