SQL в диапазоне дат возвращает неверный результат - PullRequest
0 голосов
/ 03 ноября 2019

Я пишу SQL-запрос, чтобы получить строки между форматами диапазона дат: мм / дд / гггг

Я использовал оператор 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 ]

3 голосов
/ 03 ноября 2019

Я предполагаю, что вы используете 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 голос
/ 03 ноября 2019

Я просто хочу добавить, что я использовал YYYYMMDD в своем коде SQL, потому что это (кажется) всегда работает (ы) даже без форматирования, и, поверьте мне, у меня возникли проблемы с 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
1 голос
/ 03 ноября 2019

Вы явно путаете LIKE групповые символы с другими сравнениями. '%' работает только с LIKE и NOT 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;

Нет необходимости преобразовывать даты в строку для таких сравнений. Прямые сравнения позволяют:

  • Преобразование дат в строки приводит к накладным расходам в каждой обрабатываемой строке.
  • Оптимизатор запросов для выбора соответствующих индексов, если они доступны.
  • Оптимизатор запросов для выбора соответствующих разделов, если он доступен.
  • Оптимизатор запросов для выбора соответствующих алгоритмов, поскольку статистика размеров, вероятно, будет более точной.
1 голос
/ 03 ноября 2019

просто установите формат даты и времени

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

WHERE TrainingType = 'Обучение по медицинским стажировкам' И ГОД (CONVERT (date, Internship_StartDate1,103)) МЕЖДУ '2019' И '2020' Order By Internship_StartDate1 desc

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...