我想按当前日期之后的日期过滤过期日期。问题在于当前数据的日期在三列中。 exp0year
,exp0mo
和exp0da
。
我尝试放置多个where子句,说exp0mo > 4
的问题使像1/1/2020
这样的日期无效。
Select exp0yr,exp0mo,exp0da,policy0num,zip0post,add0line01,add0line03,add0line04,profit0ctr
from ni.pmsp0200
where exp0yr>118 and exp0mo>04 and exp0da>20 and profit0ctr='eoh'
group by exp0yr,exp0mo,exp0da,policy0num,zip0post,add0line01,add0line03,add0line04,profit0ctr
我希望得到05/21/2019
之后的日期,但取而代之的是除01/01/2020
-05/20/2020
之外的所有日期。
答案 0 :(得分:0)
如果您使用的是SQL Server 2012 + 您可以获取当前数据日期as follow
datefromparts(exp0yr, exp0mo, exp0da)
否则,请使用类似这样的东西:
SELECT CAST(CONCAT(CAST(@exp0yr AS VARCHAR(4)), '-',CAST(exp0mo AS VARCHAR(2)), '-',CAST(exp0daAS VARCHAR(2))) AS DATE)
您的脚本变成这样的东西
declare @mydate as date
set @mydate='2019/05/21' --Put your target date here
Select exp0yr,exp0mo,exp0da,policy0num,zip0post,add0line01,add0line03,add0line04,profit0ctr
from ni.pmsp0200
where datefromparts(exp0yr, exp0mo, exp0da)>@mydate and profit0ctr='eoh'
group by exp0yr,exp0mo,exp0da,policy0num,zip0post,add0line01,add0line03,add0line04,profit0ctr
答案 1 :(得分:0)
尝试这个
--Temp table
CREATE TABLE #Test
(cYear VARCHAR(4) NOT NULL,
cMonth VARCHAR(2) NOT NULL ,
cDay VARCHAR(2) NOT NULL
);
--Sample data
INSERT INTO #Test
SELECT '2019',
'04',
'01'
UNION ALL
SELECT '2018',
'06',
'01';
--Calculated column - you can define prefix, such as leading zeros
ALTER TABLE #TEST
ADD calcDate AS CYEAR + CMONTH + CDAY PERSISTED
--Shows only one record
SELECT *
FROM #Test
WHERE calcDate BETWEEN '20190101' AND '20191231';