龙空技术网

SQLServer 学习笔记3 日期函数

北国撒欢的扁豆 141

前言:

目前你们对“sqlserver获取上个月最后一天”大体比较珍视,大家都需要学习一些“sqlserver获取上个月最后一天”的相关资讯。那么小编在网摘上收集了一些有关“sqlserver获取上个月最后一天””的相关知识,希望小伙伴们能喜欢,同学们快快来学习一下吧!

获取当月天数

SELECT DATEPART(MM,GETDATE()) AS '月',32 - DAY(GETDATE() + 32 - DAY(GETDATE())) AS '天数'
获取上个月天数
SELECT DATEPART(MM,GETDATE()) -1 AS '月',DAY(GETDATE() - DAY(GETDATE())) AS '天数'
获取当月剩余多少天
SELECT DATEPART(MM,GETDATE()) AS '月', (32 - DAY(GETDATE() + 32 - DAY(GETDATE()))) - DAY(GETDATE()) AS '剩余天数'
获取指定月份的天数
DECLARE @FMONTH VARCHAR(6)SET @FMONTH = '202304'SELECT @FMONTH AS '年月', DAY(DATEADD(MONTH,1,@FMONTH+'01') - 1) AS '天数'

输出结果

统计本周的销量数据

SELECT CAST(SUM(t1.FQTY) AS DECIMAL(18,2)) AS '本周'FROM T_SAL_ORDER t0INNER JOIN T_SAL_ORDERENTRY t1 ON t0.FID = t1.FIDWHERE DATEDIFF(WEEK,t0.FDATE, GETDATE()) = 0
统计本月的销量数据
SELECT CAST(SUM(t1.FQTY) AS DECIMAL(18,2)) AS '本月'FROM T_SAL_ORDER t0INNER JOIN T_SAL_ORDERENTRY t1 ON t0.FID = t1.FIDWHERE DATEDIFF(MONTH,t0.FDATE, GETDATE()) = 0
统计上月的销量数据
SELECT CAST(SUM(t1.FQTY) AS DECIMAL(18,2)) AS '上月'FROM T_SAL_ORDER t0INNER JOIN T_SAL_ORDERENTRY t1 ON t0.FID = t1.FIDWHERE DATEDIFF(MONTH,t0.FDATE, GETDATE()) = 1
统计本年的销量数据
SELECT CAST(SUM(t1.FQTY) AS DECIMAL(18,2)) AS '本年'FROM T_SAL_ORDER t0INNER JOIN T_SAL_ORDERENTRY t1 ON t0.FID = t1.FIDWHERE DATEDIFF(YEAR,t0.FDATE, GETDATE()) = 0
统计本季度销量数据
SELECT CAST(SUM(t1.FQTY) AS DECIMAL(18,2)) AS '本季度'FROM T_SAL_ORDER t0INNER JOIN T_SAL_ORDERENTRY t1 ON t0.FID = t1.FIDWHERE DATEPART(QQ,t0.FDATE) = DATEPART (QQ, GETDATE()) AND DATEPART(YY,t0.FDATE) = DATEPART(YY, GETDATE())

输出结果

标签: #sqlserver获取上个月最后一天