我已经说过,开始和结束日期分别为“mm / dd / yyyy”格式的“6/11/1996”和“3/1/2002”。 我需要得到如下所示的所有月度期。
Start Date End Date
6/11/1996 - 6/30/1996
7/01/1996 - 7/31/1996
8/01/1996 - 8/31/1996
.
.
.
Till
2/01/2002 - 2/28/2002
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
您可以对connect by
和add_months
函数使用months_between
查询:
with p as ( select date '1996-06-11' d1, date '2002-03-01' d2 from dual )
select greatest(trunc(add_months(d1, level - 1), 'month'), d1) as d1,
trunc(add_months(d1, level), 'month') - 1 as d2
from p connect by level <= months_between(trunc(d2, 'month'), trunc(d1, 'month'))
输出完全符合要求。
答案 1 :(得分:1)
假设间隔是通过两个绑定变量:from_dt
和:to_dt
(指定格式的字符串)给出的:
with
inputs ( f_dt, t_dt ) as (
select to_date(:from_dt, 'mm/dd/yyyy'), to_date(:to_dt, 'mm/dd/yyyy') from dual
),
ld ( l_day, lvl ) as (
select add_months(last_day(f_dt), level - 1), level
from inputs
connect by level <= months_between(last_day(t_dt), last_day(f_dt)) + 1
)
select case when ld.lvl = 1 then i.f_dt else add_months(ld.l_day, -1) + 1 end
as start_date,
least(i.t_dt, ld.l_day) as end_date
from inputs i cross join ld
;
这假设您在原始帖子中实际上意味着还有一个间隔,从2002年3月1日到2002年3月1日;并且查询正确处理当日期和日期在同一个月时的情况:如果输入是6/11/1996到6/21/1996,则输出正是该间隔。
添加:在因子子查询的声明中创建列别名(在WITH子句中),正如我所做的那样,需要Oracle 11.2或更高版本。对于早期版本,有必要以不同的方式编写它,如下所示:
with
inputs as (
select to_date(:from_dt, 'mm/dd/yyyy') as f_dt,
to_date(:to_dt , 'mm/dd/yyyy') as t_dt
from dual
),
ld as (
select add_months(last_day(f_dt), level - 1) as l_day, level as lvl
from inputs ...............
答案 2 :(得分:0)
public abstract class MyLangActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Locale locale = // get the locale to use...
Configuration conf = getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= 17) {
conf.setLocale(locale);
} else {
conf.locale = locale;
}
DisplayMetrics metrics = getResources().getDisplayMetrics();
getResources().updateConfiguration(conf, metrics);
super.onCreate(savedInstanceState);
}
}
上面的内容可以提供帮助。