简体   繁体   English

使用SQL Server中的通用表表达式计算月份数,月份名称,月份的第一天(日期)和月份的最后一天(日期)?

[英]Calculate month number,month name, first day of month(date) and last day of month(date) using common table expression in sql server?

declare @start_date as datetime = '12/31/2014'
declare @end_date as datetime = '02/15/2015'

;with cte as (
   select 
       datename(month,@start_date) as [mnth_nm],
       month(@start_date) as [mnth_no],
       @start_date as dat,
       DATEADD(DAY, -1 * DAY(@start_date) + 1, @start_date) as [first_day],
       DATEADD(dd, -DAY(DATEADD(mm, 1, @start_date)), DATEADD(mm, 1, @start_date)) as [last_day]
   union all
   select 
      datename(month, DateAdd(Month, 1, dat)),
      month(dat) + 1 as [mnth_no],
      DateAdd(Month, 1, dat), 
      DATEADD(MONTH, 1, [first_day]), 
      DATEADD(dd, -DAY(DATEADD(mm, 1, dat)), 
      DATEADD(mm, 1, dat)) + 1 
   from 
      cte
   where 
      DateAdd(Month,1,dat) < @end_date
)
select 
    [mnth_nm], [mnth_no], @start_date, [first_day], [last_day] 
from 
    CTE

In above CTE the required output should give december, january and february month output based on my start and end date. 在上述CTE中,所需的输出应基于我的开始和结束日期给出12月,1月和2月的输出。

Change the where query only:- 更改仅where查询:

DateAdd(Month,0,dat)< @end_date DateAdd(Month,0,dat)<@结束日期

<b>Got the solution ... </b>
<p>
declare @start_date as datetime = '12/31/2014'
declare @end_date as datetime = '03/15/2015'

;with cte as (
   select 
       datename(month,@start_date) as [mnth_nm],
       month(@start_date) as [mnth_no],
       @start_date as dat,
       DATEADD(DAY, -1 * DAY(@start_date) + 1, @start_date) as [first_day],
       DATEADD(dd, -DAY(DATEADD(mm, 1, @start_date)), DATEADD(mm, 1, @start_date)) as [last_day]
   union All
   select 
      datename(month, DateAdd(Month, 1, dat)),
      month(DateAdd(m,1,dat)) as [mnth_no],
      DateAdd(Month, 1, dat), 
      DATEADD(MONTH, 1, [first_day]), 
DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, DATEADD(MONTH, 1, [first_day])) + 1, 0)) 
   from 
      cte
   where 
    DateAdd(Month,0,dat)<  @end_date
)
select 
    [mnth_nm], [mnth_no], @start_date, [first_day], [last_day] 
from 
    CTE</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将月份名称转换为日期中的月份号,日期和月份为两位数 - Convert month name to month number in a date, two digits day and month 如果该月不存在该日期,如何更改 SQL Server 中的日期并设置该月的最后一天 - How to change day of date in SQL server and set last day of month if the day does not exist in the month SQL - 如何判断日期是否是该月的最后一天? - SQL - How to tell if a date is the last day of the month? 如何从 SQL 服务器中的给定月份和年份获取月份的第一天和最后一天 - How to get the First day and last day of the month from given month number and year in SQL Server SQL开始日期是当前日期,然后结束日期是1个月前,即使不是从该月的第一天到最后一天 - SQL Start date is current day then end date is 1 month ago even if its not from the first to the last of the month 带有日期,日期,月份,年份,期间,周数的SQL表? (SQL) - SQL table with Day, Date, Month, Year, Period, Week Number? (SQL) SQL 服务器本月最后一天 - SQL Server Last Day of the Month 在sql server中将年月日转换为日期 - Converting year month day to date in sql server 获取所选日期月份的第一天到最后一天之间的数据 - get data between first day and last day of month of selected date SQL-一个月的最后一天 - SQL - Last Day of Month
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM