Compare time part of DateTime data type in SQL Server 2005

前端 未结 6 1895
梦谈多话
梦谈多话 2021-01-02 07:41

How can I compare only the time portion of a DateTime data type in SQL Server 2005? For example, I want to get all records in which MyDateField is after a specific time. The

6条回答
  •  天命终不由人
    2021-01-02 07:53

    How about this?

    SELECT (fields)
    FROM dbo.YourTable
    WHERE DATEPART(HOUR, MyDate) >= 12
      AND DATEPART(MINUTE, MyDate) >= 23
      AND DATEPART(SECOND, MyDate) >= 45
    

    The hour is given in the 24-hour format, e.g. 12 means 12 hour noon, 15 means 3pm.

    DATEPART also has "parts" for minutes, seconds and so forth. You can of course use as many of those date parts in your WHERE clause as you like.

提交回复
热议问题