日期范围之间的SQL返回错误的结果

时间:2019-11-03 10:18:44

标签: sql sql-server

我正在编写一个SQL查询以获取日期范围格式之间的行:mm / dd / yyyy

我使用了between运算符,但结果显示超出了日期范围

Select Internship_StartDate1
From [Media_BPM].[dbo].[BP_UGME]
Where TrainingType = 'Medical Internship Training'
  and Internship_StartDate1 Between '01/%/2020%'  AND '06/%/2020%'
Order By Internship_StartDate1 desc

预期结果:仅日期为2020年

实际结果是:

Internship_StartDate1
05/16/2018
05/06/2018
05/05/2019
05/05/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/02/2019
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2020
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2019
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2018
05/01/2017
04/30/2018
04/29/2018
04/18/2018
04/14/2017
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2020
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2019
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2018
04/01/2017
04/01/2017
04/01/2017
04/01/2017
03/19/2019
03/18/2018
03/04/2018
03/03/2019
03/03/2019
03/03/2019
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2020
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2019
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2018
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
03/01/2017
02/05/2017
02/03/2019
02/03/2019
02/03/2019
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2020
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2019
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2018
02/01/2017
02/01/2017
02/01/2017
02/01/2017
02/01/2017
02/01/2017
02/01/2017
02/01/2017
02/01/2017
02/01/2017
02/01/2017
02/01/2017
02/01/2017
01/20/2019
01/19/2019
01/19/2019
01/13/2019
01/12/2019
01/12/2019
01/12/2019
01/10/2019
01/10/2019
01/10/2018
01/09/2019
01/09/2019
01/09/2017
01/08/2018
01/02/2019
01/02/2019
01/02/2019
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2020
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2019
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2018
01/01/2017
01/01/2017
01/01/2017
01/01/2017
01/01/2009

4 个答案:

答案 0 :(得分:3)

我假设您使用的是SQL Server。
在这种情况下,也许您认为%是通配符,表示任何字符,但事实并非如此。
您可以将%与运算符LIKE一起使用,但不能与BETWEEN一起使用。
使用功能FORMAT()

Select Internship_StartDate1 
from [Media_BPM].[dbo].[BP_UGME] 
where TrainingType = 'Medical Internship Training' 
AND FORMAT(Internship_StartDate1, 'yyyyMM') Between '202001' AND '202006' 
Order By Internship_StartDate1 desc

答案 1 :(得分:1)

只需设置日期时间的格式

 Select Internship_StartDate1 from [Media_BPM].[dbo].[BP_UGME]

WHERE TrainingType ='医学实习培训'   '2019'和'2020'之间的AND YEAR(转换(日期,Internship_StartDate1,103))    通过Internship_StartDate1 desc订购

答案 2 :(得分:1)

您显然将LIKE通配符与其他比较混淆。 '%'仅适用于LIKENOT LIKE

更重要的是,对日期使用日期比较!这对于优化非常重要。您应该将条件写为:

select Internship_StartDate1
from [Media_BPM].[dbo].[BP_UGME]
where TrainingType = 'Medical Internship Training' and
      Internship_StartDate1 >= '2020-01-01' and
      Internship_StartDate1 < '2020-07-01'
order by Internship_StartDate1 desc;

进行此类比较无需将日期转换为字符串。直接比较允许:

  • 将日期转换为字符串会增加处理的每一行的开销。
  • 查询优化器以选择适当的索引(如果可用)。
  • 查询优化器以选择适当的分区(如果可用)。
  • 查询优化器选择适当的算法,因为大小统计信息可能更准确。

答案 3 :(得分:1)

我只想补充一点,我最终在我的SQL代码中使用了YYYYMMDD,因为即使没有格式化,它(似乎)也总是可以工作,并且相信我在MDY(MS默认)与DMY(例如德国)很多。因此,对于整个2020年,我会这样搜索:

select Internship_StartDate1 
  from Media_BPM.dbo.BP_UGME
 where TrainingType = 'Medical Internship Training' and
       --  first and last time tick in 2020
       Internship_StartDate1 between '20200101' and '20201231 23:59:59.997'
 order by Internship_StartDate1 desc