Задать вопрос
  • Как сделать сравнение поля даты по дню месяца и месяцу, без года в SQL?

    AgentSmith
    @AgentSmith
    Это мой правильный ответ на твой вопрос
    select date from visits v
    where Year(date) in (2020, 2019, 2021) 
      and (convert(nvarchar, date, 112) % 10000) between 0101 and 0202
    order by date desc


    С переменными:
    DECLARE @mm1 int
    DECLARE @dd1 int
    DECLARE @mm2 int
    DECLARE @dd2 int
    set @mm1 = 01 -- Jan
    set @dd1 = 01 -- 01 Jan
    set @mm2 = 02 -- Feb
    set @dd2 = 02 -- 02 Feb
    select date from visits s
    where Year(date) in (2020, 2019, 2021) and (convert(nvarchar, date, 112) % 10000) between @mm1*100+@dd1 and @mm2*100+@dd2
    order by date desc

    Моя тестовая выборка:
    2021-02-02 16:29:35.867	20210202
    2021-02-02 09:53:18.180	20210202
    2021-02-01 11:49:41.843	20210201
    2021-02-01 10:52:26.090	20210201
    2021-02-01 05:59:25.413	20210201
    2021-01-31 17:16:27.647	20210131
    2021-01-31 01:28:13.800	20210131
    2021-01-29 22:47:28.280	20210129
    2021-01-28 01:18:43.880	20210128
    2021-01-27 17:00:32.680	20210127
    2021-01-26 17:03:06.370	20210126
    2021-01-26 13:36:43.130	20210126
    2021-01-25 02:43:04.183	20210125
    2021-01-23 18:06:38.257	20210123
    2021-01-22 21:28:22.670	20210122
    2021-01-22 11:14:00.070	20210122
    2021-01-22 03:39:02.317	20210122
    2021-01-21 23:50:52.460	20210121
    2021-01-20 10:33:43.037	20210120
    2021-01-19 12:50:33.563	20210119
    2021-01-18 13:07:06.797	20210118
    2021-01-18 05:30:57.800	20210118
    2021-01-17 12:04:39.160	20210117
    2021-01-16 01:37:55.150	20210116
    2021-01-16 01:37:38.803	20210116
    2021-01-15 10:00:54.527	20210115
    2021-01-15 07:12:49.957	20210115
    2021-01-14 20:10:41.240	20210114
    2021-01-14 12:57:17.180	20210114
    2021-01-13 01:58:06.477	20210113
    2021-01-11 21:28:36.753	20210111
    2021-01-11 18:32:49.987	20210111
    2021-01-10 11:02:33.037	20210110
    2021-01-10 00:57:49.190	20210110
    2021-01-10 00:31:43.147	20210110
    2021-01-09 21:14:39.497	20210109
    2021-01-09 14:47:53.053	20210109
    2021-01-07 06:37:50.820	20210107
    2021-01-06 23:58:01.973	20210106
    2021-01-06 23:01:30.507	20210106
    2021-01-06 19:25:04.980	20210106
    2021-01-05 14:27:57.190	20210105
    2021-01-05 02:17:26.830	20210105
    2021-01-04 02:13:58.767	20210104
    2021-01-01 01:44:09.970	20210101
    2020-02-01 01:12:04.120	20200201
    2020-01-30 21:01:31.607	20200130
    2020-01-29 15:12:48.213	20200129
    2020-01-29 03:38:26.007	20200129
    2020-01-25 18:58:35.283	20200125
    2020-01-25 11:28:02.890	20200125
    2020-01-24 06:59:16.207	20200124
    2020-01-23 11:50:25.683	20200123
    2020-01-23 09:48:20.953	20200123
    2020-01-11 18:31:35.483	20200111
    2020-01-10 17:02:54.343	20200110
    2020-01-09 05:16:46.150	20200109
    2020-01-08 11:01:40.010	20200108
    2020-01-03 23:31:40.690	20200103
    2019-02-02 03:14:19.380	20190202
    2019-02-01 23:27:03.230	20190201
    2019-02-01 12:04:48.313	20190201
    2019-02-01 10:53:00.440	20190201
    2019-01-31 02:00:17.017	20190131
    2019-01-30 20:36:42.050	20190130
    2019-01-30 04:51:32.180	20190130
    2019-01-29 21:28:29.593	20190129
    2019-01-29 09:06:57.633	20190129
    2019-01-28 17:17:06.380	20190128
    2019-01-28 17:11:55.363	20190128
    2019-01-28 00:45:08.617	20190128
    2019-01-27 20:39:51.217	20190127
    2019-01-27 13:53:27.733	20190127
    2019-01-25 10:34:40.563	20190125
    2019-01-24 17:42:24.610	20190124
    2019-01-23 21:19:43.660	20190123
    2019-01-23 12:47:17.670	20190123
    2019-01-23 06:20:52.970	20190123
    2019-01-22 01:49:08.037	20190122
    2019-01-21 11:33:59.607	20190121
    2019-01-20 19:46:35.057	20190120
    2019-01-20 17:32:17.583	20190120
    2019-01-20 06:28:04.137	20190120
    2019-01-19 23:47:03.213	20190119
    2019-01-19 00:52:36.540	20190119
    2019-01-18 22:57:50.220	20190118
    2019-01-18 19:35:29.507	20190118
    2019-01-18 19:30:49.370	20190118
    2019-01-18 11:29:10.717	20190118
    2019-01-17 23:13:30.620	20190117
    2019-01-17 22:58:53.320	20190117
    2019-01-17 17:02:48.607	20190117
    2019-01-17 14:40:43.887	20190117
    2019-01-17 00:50:37.983	20190117
    2019-01-15 10:27:41.050	20190115
    2019-01-15 10:05:16.363	20190115
    2019-01-15 00:00:02.663	20190115
    2019-01-14 23:57:37.637	20190114
    2019-01-14 20:44:05.003	20190114
    2019-01-14 14:35:01.787	20190114
    2019-01-13 19:19:11.040	20190113
    2019-01-13 13:08:33.310	20190113
    2019-01-13 00:24:35.660	20190113
    2019-01-12 08:21:46.990	20190112
    2019-01-11 19:23:17.230	20190111
    2019-01-10 10:35:41.510	20190110
    2019-01-08 22:20:53.743	20190108
    2019-01-08 02:36:35.820	20190108
    2019-01-07 20:42:08.933	20190107
    2019-01-06 19:38:55.173	20190106
    2019-01-06 13:09:38.390	20190106
    2019-01-05 22:40:39.083	20190105
    2019-01-05 16:35:22.310	20190105
    2019-01-04 17:57:56.823	20190104
    2019-01-04 17:55:29.443	20190104
    2019-01-04 17:52:43.973	20190104
    2019-01-04 12:04:57.853	20190104
    2019-01-04 10:17:43.190	20190104
    2019-01-03 23:25:11.503	20190103
    2019-01-02 16:33:22.460	20190102
    2019-01-02 16:22:01.097	20190102
    Ответ написан
    3 комментария