Take the time from the datetime and convert it into seconds?

后端 未结 3 1691
故里飘歌
故里飘歌 2021-02-13 18:52

I am running SQL Server 2005. Technically I know how to take the time from a tsql datetime.

CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond
相关标签:
3条回答
  • 2021-02-13 19:03

    Use DATEPART:

    (DATEPART(HOUR, GETDATE()) * 3600) +
    (DATEPART(MINUTE, GETDATE()) * 60) +
    (DATEPART(SECOND, GETDATE()))
    
    0 讨论(0)
  • 2021-02-13 19:04

    Just my 2 cents ...another way of doing this

    Edit: Added method for SQL Server 2005 (Thank you Michael)

    for SQL Server 2008

    SELECT DATEDIFF(SECOND, CONVERT(date, GETDATE()), GETDATE())
    

    for sql server 2005+

    SELECT DATEDIFF(SECOND, DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())), GETDATE())
    
    0 讨论(0)
  • 2021-02-13 19:15

    The functions you want is DatePart

    Declare @d DateTime
    Select @d = GetDate()
    Select (DatePart(HOUR, @d) * 3600) + (DatePart(MINUTE, @d) * 60) + DatePart(SECOND, @d)
    
    0 讨论(0)
提交回复
热议问题