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

后端 未结 17 1884
隐瞒了意图╮
隐瞒了意图╮ 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 06:42
    select * from invoice where TRANS_DATE_D>= to_date  ('20170831115959','YYYYMMDDHH24MISS')
    and TRANS_DATE_D<= to_date  ('20171031115959','YYYYMMDDHH24MISS');
    
    0 讨论(0)
  • 2020-12-02 06:43

    You can use this approach which truncates the time part:

    select * from test
    where convert(datetime,'03/19/2014',102) = DATEADD(dd, DATEDIFF(dd, 0, date), 0)
    
    0 讨论(0)
  • 2020-12-02 06:45
    -- Reverse the date format
    -- this false:
        select * from test where date = '28/10/2015'
    -- this true:
        select * from test where date = '2015/10/28'
    
    0 讨论(0)
  • 2020-12-02 06:49
    select *
      from invoice
     where TRUNC(created_date) <=TRUNC(to_date('04-MAR-18 15:00:00','dd-mon-yy hh24:mi:ss'));
    
    0 讨论(0)
  • 2020-12-02 06:50
    select *, cast ([col1] as date) <name of the column> from test where date = 'mm/dd/yyyy'
    

    "col1" is name of the column with date and time
    <name of the column> here you can change name as desired

    0 讨论(0)
  • 2020-12-02 06:52

    Simply use this in your WHERE clause.

    The "SubmitDate" portion below is the column name, so insert your own.

    This will return only the "Year" portion of the results, omitting the mins etc.

    Where datepart(year, SubmitDate) = '2017'
    
    0 讨论(0)
提交回复
热议问题