Compare time part of DateTime data type in SQL Server 2005

前端 未结 6 1924
梦谈多话
梦谈多话 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 08:00

    Here's the SQL to match date with time:

    to match only time:

    AND CAST(RIGHT(CONVERT(VARCHAR, YOURDATE, 100), 7) as time) >
    CAST(RIGHT(CONVERT(VARCHAR, GETDATE(), 100), 7) as time)
    

    to match date with time:

    CAST(YOURDATE AS DATE)=CAST(GETDATE() AS DATE)
    AND CAST(RIGHT(CONVERT(VARCHAR, YOURDATE, 100), 7) as time) >
    CAST(RIGHT(CONVERT(VARCHAR, GETDATE(), 100), 7) as time)
    

提交回复
热议问题