with t as
(select rownum - 1 rn from dual connect by rownum <= 1000)
select to_char(add_months(trunc(sysdate), -rn), 'yyyy-mm')
from t
where to_char(add_months(trunc(sysdate), -rn), 'yyyy') between 2017 and 2018
order by to_char(add_months(trunc(sysdate), -rn), 'yyyy-mm')
结果:
修改语句中2017和2018的地方就行,最大支持查询1000行数据,要是再多的话,修改语句第二行<=1000的那个位置。
思路:先获取年份和周数,然后获取该年第一天是周几, 然后获取该年第一周的开始时间,然后加上(周数-1)7,例子
with tmp as(select '1502' as a
from dual
union all
select '1503' as a
from dual
union all
select '1402' as a
from dual
union all
select '0701' as a from dual)
select a,
to_char(years - w + (weeks - 1) 7,'yyyymmdd') as begin_week
from (select a,
years,
weeks,
--因为oracle 的周是从周日开始,星期一是2
to_char(years, 'd') - 2 as w
from (select a,
to_date('20' || substr(a, 1, 2) || '-01-01',
'yyyy-mm-dd') as years,
substr(a, -2, 2) as weeks
from tmp))
sqlserver:
select convert(varchar(7),dateadd(mm,-tnumber,getdate()),120)from
(select number from masterspt_values where type='P') t
where year(dateadd(mm,-tnumber,getdate()))=year(getdate())
order by convert(varchar(7),dateadd(mm,-tnumber,getdate()),120)
oracle:
select to_char(add_months(sysdate, -trn), 'yyyy-mm')from dual a, (select rownum - 1 rn from dual connect by rownum <= 12) t
where to_char(add_months(sysdate, -trn), 'yyyy') =
to_char(sysdate, 'yyyy')
order by to_char(add_months(sysdate, -trn), 'yyyy-mm')
to_date(to_char(the_date,'yyyymmdd'),'the_format_you_need');
the_format_you_need='YYYY-MM-DD' or 'RR-MM-DD' or 'YYYY/MM/DD' or others
1、查询确定年月的数据,使用to_date或to_char都可以实现
SELECT FROM 表名WHERE TO_CHAR(日期列,'YYYY-MM') = TO_CHAR('2013-06','YYYY-MM') 或者
SELECT FROM 表名
WHERE 日期列 = TO_Date('2013-06','YYYY-MM')
2、查询前一个月
select concat(to_char(sysdate,'yyyy')||to_char(add_months(sysdate,-1), '-MM-'),'01 00:00:00')start_time,concat(to_char(sysdate,'yyyy')||to_char(add_months(sysdate,-1), '-MM-')||to_char(LAST_DAY(sysdate),'dd '),'23:59:59') end_time from dual
3、查询前一天
select to_char(sysdate-1,'yyyy-mm-dd ')||'00:00:00' start_time,to_char(sysdate-1,'yyyy-mm-dd ')||'23:59:59' end_time from dual以上就是关于Oracle:根据输入的年份得到从当年1月到当前月的所有年月时间全部的内容,包括:Oracle:根据输入的年份得到从当年1月到当前月的所有年月时间、oracle 中 如何将年周(例如1502)转换为年月日(20150105)、SQLServer、Oracle获取当前年份的1月到当前月之间的所有月份等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)