Oracle数据库中的表用varchar字段存取年月,前端传来String类型字段来查与之匹配的前一年数据。
因为是需要前一年(12个月的数据)所有需要把String转为Date
select t.* from tableName t where t.username= #{xxxx} and (to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -1)
or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -2) or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -3)
or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -4) or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -5)
or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -6) or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -7)
or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -8) or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -9)
or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -10) or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -11)
or to_date(t.xxxx, 'yyyyMM') = add_months(trunc(to_date(#{xxxx}, 'yyyyMM'), 'MM'), -12))
- to_date(#{xxxx},‘yyyyMM’),这个yyyyMM(不区分大小写)代表你xxxx的长度,如你的xxxx是202106它将帮你转换为2021/6/1
- trunc(to_date(xxxx,‘yyyymm’),‘mm’),trunc(日期)函数时按月精确,返回当月的第一天
- add_months(trunc(to_date(xxxx,‘yyyymm’),‘mm’),2)对月份加2