How to cast the DateTime to Time

后端 未结 3 828
我寻月下人不归
我寻月下人不归 2020-11-22 02:52

I am casting DateTime field to Time by using CAST Syntax.

select CAST([time] as time) as [CSTTime]

DateTime

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 03:01

    SQL Server 2008:

    select cast(MyDate as time) [time] from yourtable
    

    Earlier versions:

    select convert(char(5), MyDate , 108) [time] from yourtable
    

    Other Options:

    SELECT CONVERT(VARCHAR(20), GETDATE(), 114)
    

    The simplest way to get the time from datetime without millisecond stack is:

    SELECT CONVERT(time(0),GETDATE())
    

    Hour and Minute

    SELECT substring(CONVERT(VARCHAR, GETDATE(), 108),0,6) AS Time
    

提交回复
热议问题