SQL查询问题
select *
from app_contract_hd
where year(created_datetime) between 2018 and 2018 and MONTH(created_datetime)between 4 and 4
答案 0 :(得分:1)
如果您的'created_datetime'
列是'datetime'
数据类型,则可以使用DATEFROMPARTS()函数尝试下一种方法:
SELECT *
FROM app_contract_hd
WHERE CONVERT(date, created_datetime) BETWEEN DATEFROMPARTS(2018, 1, 1) AND DATEFROMPARTS(2019, 1, 1)
此外,如果您想按特定时间过滤数据,请使用DATETIMEFROMPARTS():
SELECT *
FROM app_contract_hd
WHERE created_datetime BETWEEN DATEFROMPARTS(2018, 1, 1, 0, 0, 0, 0) AND DATEFROMPARTS(2019, 1, 1, 12, 0, 0, 0, 0)
答案 1 :(得分:0)
如果数据类型为日期时间
select * from app_contract_hd
where year(created_datetime) between cast('2018-01-01 00:00:00' as datetime) and cast('2019-01-31 23:59:59'
as datetime)
答案 2 :(得分:0)
您可以尝试简单的代码:
select *
from app_contract_hd
where '2018-01-01' <= created_datetime and created_datetime < '2019-02-01'
答案 3 :(得分:0)
当您在created_datetime字段中有TIME时,如果您用原始的上限日期将+1天以内的上限检查为宜,则效果会更好
SELECT *
FROM app_contract_hd
WHERE created_datetime >= '20180101'
AND created_datetime < '20180201'
答案 4 :(得分:0)
您可以尝试
SELECT * FROM APP_CONTRACT_HD
WHERE
(YEAR(CREATED_DATETIME) >=2018 AND MONTH(CREATED_DATETIME)>=1)
OR
(YEAR(CREATED_DATETIME) <=2019 AND MONTH(CREATED_DATETIME)<=1)
答案 5 :(得分:0)
使用CONCAT
并使用BETWEEN
检查的一种可能方式
SELECT *
FROM app_contract_hd
WHERE CONCAT(YEAR(created_datetime), MONTH (created_datetime)) BETWEEN 201801 AND 201901