我有四个变量 CurrentMonth、CurrentYear、Month、year.我想要一个 SQL 语句,它显示 (Month,Year) 到 (currentMonth,CurrentYear) 之间的所有月份和年份.下面显示月份,但我需要月份和年份,我没有日期,但只有开始和结束的月份和年份.
I have four variables CurrentMonth, CurrentYear, Month, year. I want to have a SQL statement which displays all the months and years between (Month,Year) to (currentMonth,CurrentYear). The below displays months but I need months and year and I dont have dates but only month and year of start and End.
DECLARE @StartDate DATETIME,
@EndDate DATETIME;
SELECT @StartDate = '20110501'
,@EndDate = '20110801';
SELECT DATENAME(MONTH, DATEADD(MONTH, x.number, @StartDate)) AS MonthName
FROM master.dbo.spt_values x
WHERE x.type = 'P'
AND x.number <= DATEDIFF(MONTH, @StartDate, @EndDate);
这是对上一个回答的修改,来自 indicently 以满足您的月/年需求.
This is a modification from the previous answer from indicently to meet your Month/Year needs.
DECLARE @FromMonth varchar(2), @FromYear varchar(4), @ToMonth as Varchar(2), @ToYear as varchar(4)
SET @FromMonth = '01';
SET @FromYear = '1944';
SET @ToMonth = '04';
SET @ToYear = '1956';
DECLARE @DateFrom smalldatetime, @DateTo smalldatetime;
SET @DateFrom=@FromYear+@FromMonth+'01';
SET @DateTo=@ToYear+@ToMonth+'01';
-------------------------------
WITH T(date)
AS
(
SELECT @DateFrom
UNION ALL
SELECT DateAdd(month,1,T.date) FROM T WHERE T.date < @DateTo
)
SELECT datename(m,date) + '-' + datename(yy,date) as DN FROM T
OPTION (MAXRECURSION 32767);
这篇关于SQL 显示两个日期之间的月份和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
修改现有小数位信息Modify Existing decimal places info(修改现有小数位信息)
多次指定相关名称“CONVERT"The correlation name #39;CONVERT#39; is specified multiple times(多次指定相关名称“CONVERT)
T-SQL 左连接不返回空列T-SQL left join not returning null columns(T-SQL 左连接不返回空列)
从逗号或管道运算符字符串中删除重复项remove duplicates from comma or pipeline operator string(从逗号或管道运算符字符串中删除重复项)
将迭代查询更改为基于关系集的查询Change an iterative query to a relational set-based query(将迭代查询更改为基于关系集的查询)
将零连接到 sql server 选择值仍然显示 4 位而不是concatenate a zero onto sql server select value shows 4 digits still and not 5(将零连接到 sql server 选择值仍然显示 4 位而不是 5)