在SQL中在两个日期之间创建日期

时间:2018-08-13 15:09:40

标签: sql db-browser-sqlite

我正在使用DBite for SQLite,并编写了一个查询:

SELECT "Trade Details 2".TRADE_TYPOLOGY,
        "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".COUNTERPARTYNAME,
        "Trade Details".TERM
        FROM "Trade Details 2"
        JOIN "Trade Details"
        WHERE "Trade Details 2".CONTRACT_ID="Trade Details".FC_ID
        ORDER BY "Trade Details 2".MATURITY_DATE desc

我的目标是确定MATURITY_DATE的数量,这样,当我运行SQL查询时,我只会得到MATURITY_DATE在2018-06-22至2099-12-30之间的交易。但是,我不确定要添加什么行以实现此目标。但是,当我添加行

2099-12-30>"Trade Details 2".MATURITY_DATE>2018-06-22

按语句排序上方,在where语句下方,运行时没有任何输出。我将如何正确执行此操作?

1 个答案:

答案 0 :(得分:1)

我认为您可以尝试将AND中的BETWEENwhere一起使用。

SELECT "Trade Details 2".TRADE_TYPOLOGY,
        "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".COUNTERPARTYNAME,
        "Trade Details".TERM
        FROM "Trade Details 2"
        JOIN "Trade Details"
WHERE 
   "Trade Details 2".CONTRACT_ID="Trade Details".FC_ID
AND 
   "Trade Details 2".MATURITY_DATE BETWEEN '2018-06-22' AND '2099-12-30'
ORDER BY "Trade Details 2".MATURITY_DATE desc

或使用大于和小于

SELECT "Trade Details 2".TRADE_TYPOLOGY,
        "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".COUNTERPARTYNAME,
        "Trade Details".TERM
        FROM "Trade Details 2"
        JOIN "Trade Details"
WHERE 
   "Trade Details 2".CONTRACT_ID="Trade Details".FC_ID
AND 
   "Trade Details 2".MATURITY_DATE > '2018-06-22' AND   "Trade Details 2".MATURITY_DATE < '2099-12-30'
ORDER BY "Trade Details 2".MATURITY_DATE desc