How do I query for all dates greater than a certain date in SQL Server?

后端 未结 5 967
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 09:18

I\'m trying:

SELECT * 
FROM dbo.March2010 A
WHERE A.Date >= 2010-04-01;

A.Date looks like: 2010-03-04 00:00:00.000

相关标签:
5条回答
  • 2020-11-27 09:47

    We can use like below as well

    SELECT * 
    FROM dbo.March2010 A
    WHERE CAST(A.Date AS Date) >= '2017-03-22';
    
    SELECT * 
        FROM dbo.March2010 A
        WHERE CAST(A.Date AS Datetime) >= '2017-03-22 06:49:53.840';
    
    0 讨论(0)
  • 2020-11-27 09:50
    select *  
    from dbo.March2010 A 
    where A.Date >= Convert(datetime, '2010-04-01' )
    

    In your query, 2010-4-01 is treated as a mathematical expression, so in essence it read

    select *  
    from dbo.March2010 A 
    where A.Date >= 2005; 
    

    (2010 minus 4 minus 1 is 2005 Converting it to a proper datetime, and using single quotes will fix this issue.)

    Technically, the parser might allow you to get away with

    select *  
    from dbo.March2010 A 
    where A.Date >= '2010-04-01'
    

    it will do the conversion for you, but in my opinion it is less readable than explicitly converting to a DateTime for the maintenance programmer that will come after you.

    0 讨论(0)
  • 2020-11-27 09:50

    Try enclosing your date into a character string.

     select * 
     from dbo.March2010 A
     where A.Date >= '2010-04-01';
    
    0 讨论(0)
  • 2020-11-27 09:53
    DateTime start1 = DateTime.Parse(txtDate.Text);
    
    SELECT * 
    FROM dbo.March2010 A
    WHERE A.Date >= start1;
    

    First convert TexBox into the Datetime then....use that variable into the Query

    0 讨论(0)
  • 2020-11-27 09:55

    To sum it all up, the correct answer is :

    select * from db where Date >= '20100401'  (Format of date yyyymmdd)
    

    This will avoid any problem with other language systems and will use the index.

    0 讨论(0)
提交回复
热议问题