SELECT "Trade Details 2".Portfolio,
"Trade Details 2".CONTRACT_ID,
"Trade Details 2".START_DATE,
"Trade Details 2".MATURITY_DATE,
"Trade Details 2".NOTIONAL1,
"Trade Details 2".CONTRACT_NPV,
"Trade Details".TERM
FROM "Trade Details 2"
JOIN "Trade Details"
WHERE "Trade Details 2".CONTRACT_ID="Trade Details".FCC_ID and
("Trade Details 2".NOTIONAL1 > "0.0") and
("Trade Details 2".MATURITY_DATE > "20180621")
ORDER BY CONTRACT_ID asc
我正在尝试为我在此查询中加入的表中的START_DATE
和MATURITY_DATE
列固定日期格式。 START_DATE
的格式类似于:2018-06-22 00:00:00
,而MATURITY_DATE
的格式类似于:20180622
。我希望能够解决此问题,因此两列的格式相同,最好是2018-06-22
。我已经在python中尝试过使用:
input_string ='20191217'
start_date=pd.to_datetime(input_string[-2:]+input_string[4:6]+
input_string[:4], dayfirst=True)
但这没有用,只是声称语法无效。在SQL或Python中,有没有一种方法可以使这两列的日期相同?
答案 0 :(得分:1)
您正在使用什么DBMS?
例如,您需要使用STR_TO_DATE('your string', '%Y-%m-%d')
来转换开始日期。
SELECT STR_TO_DATE('2018-06-22', '%Y-%m-%d') FROM DUAL;
答案 1 :(得分:0)
编辑:更改了SQLite DB的查询
SELECT "Trade Details 2".Portfolio,
"Trade Details 2".CONTRACT_ID,
DATE("Trade Details 2".START_DATE) as START_DATE,
DATE(substr("Trade Details 2".MATURITY_DATE, 0, 5) || '-' || substr("Trade Details 2".MATURITY_DATE, 5,2) || '-' || substr("Trade Details 2".MATURITY_DATE, 7, 9)) as MATURITY_DATE,
"Trade Details 2".NOTIONAL1,
"Trade Details 2".CONTRACT_NPV,
"Trade Details".TERM
FROM "Trade Details 2"
JOIN "Trade Details"
WHERE "Trade Details 2".CONTRACT_ID="Trade Details".FCC_ID and
("Trade Details 2".NOTIONAL1 > "0.0") and
("Trade Details 2".MATURITY_DATE > DATE(substr('20180602', 0, 5) || '-' || substr('20180602', 5,2) || '-' || substr('20180602', 7, 9)) )
ORDER BY CONTRACT_ID asc