How to query DATETIME field using only date in Microsoft SQL Server?

后端 未结 17 1888
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 06:31

I have a table TEST with a DATETIME field, like this:

ID NAME DATE
1 TESTING 2014-03-19 20:05:20.000


        
相关标签:
17条回答
  • 2020-12-02 07:00

    There is a problem with dates and languages and the way to avoid it is asking for dates with this format YYYYMMDD.

    This way below should be the fastest according to the link below. I checked in SQL Server 2012 and I agree with the link.

    select * from test where date >= '20141903' AND date < DATEADD(DAY, 1, '20141903');
    
    • Bad habits to kick : mis-handling date / range queries
    0 讨论(0)
  • 2020-12-02 07:00

    use this

    select * from TableName where DateTimeField > date() and  DateTimeField < date() + 1
    
    0 讨论(0)
  • 2020-12-02 07:06

    This works for me for MS SQL server:

    select * from test
    where 
    year(date) = 2015
    and month(date) = 10
    and day(date)= 28 ;
    
    0 讨论(0)
  • 2020-12-02 07:06

    Test this query.

    SELECT *,DATE(chat_reg_date) AS is_date,TIME(chat_reg_time) AS is_time FROM chat WHERE chat_inbox_key='$chat_key' 
                             ORDER BY is_date DESC, is_time DESC
    
    0 讨论(0)
  • 2020-12-02 07:06
    SELECT * FROM test where DATEPART(year,[TIMESTAMP]) = '2018'  and  DATEPART(day,[TIMESTAMP]) = '16' and  DATEPART(month,[TIMESTAMP]) = '11'
    
    0 讨论(0)
提交回复
热议问题