[英]how to retrieve month current year and same month last year based on yyyymmdd
is there a way I can retrieve the month this year and same month last year based on yyyymmdd? 有什么方法可以根据yyyymmdd检索今年的月份和去年的同一月? My report is comparing data from last year month and current year month. 我的报告正在比较去年月份和当年月份的数据。 For example if yyyymmdd is 20180613 then I would like to have dates 20180601 to 20180613 and 20170601 to 20170613. 例如,如果yyyymmdd为20180613,那么我想输入日期20180601至20180613和20170601至20170613。
Would really appreciate all your advice and help I can get. 非常感谢您的所有建议和帮助。
Thank you! 谢谢!
In Oracle - 在Oracle中-
Query - 查询-
select to_char(trunc(sysdate),'yyyymmdd') as y1, to_char(trunc(sysdate,'mm'),'yyyymmdd') as y2, to_char(trunc(sysdate) + interval '-1' year,'yyyymmdd') as y3, to_char(trunc(sysdate,'mm') + interval '-1' year,'yyyymmdd') as y4 from dual;
Output - 输出-
"Y1","Y2","Y3","Y4"
"20180613","20180601","20170613","20170601"
SQL Server SQL服务器
You can try Convert(type,datetime,112)
and DATEADD
您可以尝试Convert(type,datetime,112)
和DATEADD
declare @tdate DATETIME= CONVERT(DATETIME, '20180613', 112) ;
select CONVERT(nvarchar(20), sDate1, 112) sDate1,
CONVERT(nvarchar(20), eDate1, 112) eDate1,
CONVERT(nvarchar(20), sDate2, 112) sDate2,
CONVERT(nvarchar(20), eDate2, 112) eDate2
from (
SELECT DATEADD(day, - DAY ( @tdate ) + 1 , @tdate) sDate1,
@tdate eDate1,
DATEADD(year,-1,DATEADD(day, - DAY ( @tdate ) + 1 , @tdate)) sDate2,
DATEADD(year,-1,@tdate) eDate2
)T;
Result: 结果:
sDate1 eDate1 sDate2 eDate2
-------------------- -------------------- -------------------- --------------------
20180601 20180613 20170601 20170613
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.