Home > database >  Problem of SQL date/time - less than or equal to a time when there is no return expected results
Problem of SQL date/time - less than or equal to a time when there is no return expected results

Time:12-09

 select * from table 
WHERE
1=1
AND the CONVERT (DATETIME, [Date]) & gt;
='2020-12-01'AND the CONVERT (DATETIME, [Date]) & lt;='2020-12-08'

I expected results to return contains' 2020-12-01 'and' 2020-12-08 ', include all the results (PS: the start time and end time not sure, can't use * days ago that kind of writing; Old code, try not to suggest modify the deadline for '2020-12-09')
Local execution results are as follows:

Have bosses can know how to write back in '2020-12-08' results at the same time

CodePudding user response:

BBS active low, oneself solved:
 select * from table where 1=1 
AND the CONVERT (DATETIME, [Date]) & gt;
='2020-12-01'AND (the CONVERT (DATETIME, [Date]) & lt; (select DATEADD (day, 1, '2020-12-08')))

CodePudding user response:

reference 1st floor play Pikachu response:
BBS active low, oneself solved:
 select * from table where 1=1 
AND the CONVERT (DATETIME, [Date]) & gt;
='2020-12-01'AND (the CONVERT (DATETIME, [Date]) & lt; (select DATEADD (day, 1, '2020-12-08')))


You again not to use the 2020-12-09, you again in the 2020-12-09... Don't just use 2020-12-08 + 1...
And I tried it on, that it can take to the boundary value that
 CREATE TABLE # TA (category VARCHAR (20), date VARCHAR (20), the number of INT) 

INSERT INTO # TA values (' apple ', '2019-01-01', 20)
INSERT INTO # TA values (' banana ', '2019-01-02', 30)
INSERT INTO # TA values (' orange ', '2019-01-03', 10)


Select * from # TA where a
The CONVERT datetime, a. (date) & gt; And
='2019-01-01'The CONVERT datetime, a. (date) & lt;
='2019-01-03'
  • Related