A way to extract from a DateTime value data without seconds

前端 未结 8 2044
一向
一向 2020-11-30 13:39

I have an sql DateTime (ms sql server) and want to extract the same date without the seconds: e.g. 2011-11-22 12:14:58.000 to become: 2011-11

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 14:00

    DECLARE @TheDate DATETIME
    SET @TheDate = '2011-11-22 12:14:58.000'
    
    DATEADD(mi, DATEDIFF(mi, 0, @TheDate), 0)
    

    In queries

    /* ...all records in that minute; index-friendly expression */ 
    WHERE TheDate BETWEEN DATEADD(mi, DATEDIFF(mi, 0, @TheDate), 0) 
                      AND DATEADD(mi, DATEDIFF(mi, 0, @TheDate) + 1, 0)
    

提交回复
热议问题