select 两个字符串之间的 smalldatetime
select smalldatetime between two strings
DROP TABLE IF EXISTS b;
CREATE TABLE b(
MajDate smalldatetime
);
INSERT INTO b(MajDate) VALUES
(try_convert(smalldatetime,'2016-11-30 11:23:00')),
(try_convert(smalldatetime,'2021-07-07 11:07:00')),
(try_convert(smalldatetime,'2021-07-07 11:07:00'))
select
b.MajDate,
CASE WHEN b.MajDate BETWEEN '2021-07-01 00:00:00' AND '2021-08-01 00:00:00'
THEN 'YES'
ELSE 'NO'
END AS InRange
From b;
我做错了什么?
所需输出:InRange 列最后两行应包含 YES。
它对我有用。
我猜这个问题与您机器上的 Date/Time 设置有关。
您可能需要检查 SET DATEFORMAT ...
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, MajDate smalldatetime);
INSERT INTO @tbl (MajDate) VALUES
('2016-11-30 11:23:00'),
('2021-07-07 11:07:00'),
('2021-07-07 11:07:00');
-- DDL and sample data population, end
SET DATEFORMAT mdy;
SELECT ID, MajDate
, CASE WHEN MajDate BETWEEN '2021-07-01 00:00:00' AND '2021-08-01 00:00:00'
THEN 'YES'
ELSE 'NO'
END AS InRange
,IIF(MajDate BETWEEN '2021-07-01 00:00:00' AND '2021-08-01 00:00:00', 'YES', 'NO')AS result
FROM @tbl;
输出
+----+---------------------+---------+--------+
| ID | MajDate | InRange | result |
+----+---------------------+---------+--------+
| 1 | 2016-11-30 11:23:00 | NO | NO |
| 2 | 2021-07-07 11:07:00 | YES | YES |
| 3 | 2021-07-07 11:07:00 | YES | YES |
+----+---------------------+---------+--------+
尝试指定 ISO 格式日期,很可能是您的区域设置产生了影响。
如果您使用 YYYYMMDD 就没有歧义。以下工作正常:
select
b.MajDate,
CASE WHEN b.MajDate BETWEEN '20210701 00:00:00' AND '20210801 00:00:00' THEN 'YES' else 'NO'
END AS InRange
From b;
DROP TABLE IF EXISTS b;
CREATE TABLE b(
MajDate smalldatetime
);
INSERT INTO b(MajDate) VALUES
(try_convert(smalldatetime,'2016-11-30 11:23:00')),
(try_convert(smalldatetime,'2021-07-07 11:07:00')),
(try_convert(smalldatetime,'2021-07-07 11:07:00'))
select
b.MajDate,
CASE WHEN b.MajDate BETWEEN '2021-07-01 00:00:00' AND '2021-08-01 00:00:00'
THEN 'YES'
ELSE 'NO'
END AS InRange
From b;
我做错了什么?
所需输出:InRange 列最后两行应包含 YES。
它对我有用。
我猜这个问题与您机器上的 Date/Time 设置有关。
您可能需要检查 SET DATEFORMAT ...
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, MajDate smalldatetime);
INSERT INTO @tbl (MajDate) VALUES
('2016-11-30 11:23:00'),
('2021-07-07 11:07:00'),
('2021-07-07 11:07:00');
-- DDL and sample data population, end
SET DATEFORMAT mdy;
SELECT ID, MajDate
, CASE WHEN MajDate BETWEEN '2021-07-01 00:00:00' AND '2021-08-01 00:00:00'
THEN 'YES'
ELSE 'NO'
END AS InRange
,IIF(MajDate BETWEEN '2021-07-01 00:00:00' AND '2021-08-01 00:00:00', 'YES', 'NO')AS result
FROM @tbl;
输出
+----+---------------------+---------+--------+
| ID | MajDate | InRange | result |
+----+---------------------+---------+--------+
| 1 | 2016-11-30 11:23:00 | NO | NO |
| 2 | 2021-07-07 11:07:00 | YES | YES |
| 3 | 2021-07-07 11:07:00 | YES | YES |
+----+---------------------+---------+--------+
尝试指定 ISO 格式日期,很可能是您的区域设置产生了影响。
如果您使用 YYYYMMDD 就没有歧义。以下工作正常:
select
b.MajDate,
CASE WHEN b.MajDate BETWEEN '20210701 00:00:00' AND '20210801 00:00:00' THEN 'YES' else 'NO'
END AS InRange
From b;