获取所有时间区间,SqlServer 获取本周、本月、本季、本年的第一天和最后一天
""" 获取所有时间区间 :return: """ sql = """SELECT '{tradedate}' AS ENDDATE, dateadd(WEEK, -1, '{tradedate}')+1 AS date_week, dateadd(MONTH, -1, '{tradedate}')+1 AS date_month, dateadd(MONTH, -3, '{tradedate}')+1 AS date_quarter, dateadd(MONTH, -6, '{tradedate}')+1 AS date_halfyear, dateadd(YEAR, -1, '{tradedate}')+1 AS date_1year, dateadd(YEAR, -3, '{tradedate}')+1 AS date_3year, dateadd(YEAR, -2, '{tradedate}')+1 AS date_2year, dateadd(YEAR, -5, '{tradedate}')+1 AS date_5year, CONVERT(datetime,concat('1/1/',year('{tradedate}')),101) AS date_cyear """
--本周 select dateadd(WEEK, datediff(WEEK, 0, getdate()), 0); select dateadd(WEEK, datediff(WEEK, 0, getdate()), 7); --本月 select dateadd(month, datediff(month, 0, getdate()), 0); select dateadd(month, datediff(month, -1, getdate()), -1); --本季 select dateadd(QUARTER , datediff(QUARTER ,0, getdate()), 0); select dateadd(QUARTER , datediff(QUARTER , -1, getdate()), -1); --本年 select dateadd(year, datediff(year, 0, getdate()), 0); select dateadd(year, datediff(year, -1, getdate()), -1); ———————————————— 原文链接:https://blog.csdn.net/KrisKing/article/details/45841247