开始日期和结束日期

开始日期和结束日期

本文介绍了开始日期和结束日期之间的差异以显示另一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的输入:我有2列开始日期和结束日期根据这些列显示另一列 开始日期结束日期 8-Mar-14 12-May-14 15-Aug-14 20-Aug-14 12-May-14 15 -Aug-14 输出:MOC列应显示开始日期和结束日期之间的差异,值应以逗号分隔 b $ b 开始日期结束日期 Moc 8 -Mar-14 12月5日 - 14日march.april,可能 15-Aug-14 20-Aug-14 august 12-May-14 15-Aug- 14月5日,7月,8月,解决方案 我在这里修改的上述解决方案检查: CREATE FUNCTION [dbo] .FUN_GETMONTH( @ date1 datetime , @ dat e2 datetime ) RETURNS VARCHAR ( 1000 ) AS BEGIN 声明 @ tmp varchar (MAX) SET @ tmp = ' ' ; with cte as ( select datename(month, @ date1 ) as [Month_Name], @ date1 as dat union all 选择 datename(月,DateAdd(月, 1 ,dat)),DateAdd(Month, 1 ,dat)来自 cte 其中​​ convert ( char ( 6 ),DateAdd(Month, 1 ,dat), 112 )< = convert ( char ( 6 ), @ date2 , 112 )) 选择 @tmp = @ tmp + CASE WHEN [Month_Name] = ' 01' 那么 ' JAN' WHEN [Month_Name ] = ' 02' 那么 ' FEB' WHEN [Month_Name ] = ' 03' 那么 ' MAR' WHEN [Month_Name ] = ' 04' 那么 ' APR' WHEN [Month_Name ] = ' 05' 那么 ' MAY' WHEN [Month_Name] = ' 06' 那么 ' JUN' WHEN [Month_Name] = ' 07' 那么 ' JUL' WHEN [Month_Name] = ' 08' 那么 ' AUG' WHEN [Month_Name] = ' 09' 那么 ' SEP' WHEN [Month_Name] = ' 10' 那么 ' OCT' WHEN [Month_Name] = ' 11' 那么 ' NOV' WHEN [Month_Name] = ' 12' 那么 ' DEC' END + ' ,' 来自 CTE 选项(maxrecursion 0 ) RETURN @ TMP ; END DECLARE @ TBL 表 ( STRARTDATE DATETIME , ENDDATE DATETIME ) INSERT INTO @ TBL (STRARTDATE,ENDDATE) SELECT ' 2014-01-20',' 2014- 04-20' UNION ALL SELECT ' 2014-05-20',' 2014-09-20' UNION ALL SELECT ' 2014-07-20' ,' 2014-07-20' UNION ALL SELECT ' 2014-06-20',' 2014-11-20' 选择 STRARTDATE,ENDDATE,dbo.FUN_GETMONTH(STRARTDATE,ENDDATE) AS MONTHS 来自 @ TBL 您好, 试试这个... DECLARE @ StartDate DATETIME , @ EndDate DATETIME ; SELECT @ StartDate = ' 2014年3月8日' , @ EndDate = ' 8-May-2014'; 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 ); 干杯 下面的功能可以解决问题。首先创建以下函数 / ******对象:UserDefinedFunction [dbo]。[FN_GET_DEFORM_MONTHS]脚本日期:07/01 / 2014 12:14:51 ****** / SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo] .FN_GET_DEFORM_MONTHS(@START_MONTH INT,@ END_MONTH INT) RETURNS VARCHAR(1000) AS BEGIN DECLARE @RetVal VARCHAR(1000) SELECT @RetVal ='' ; WITH n(n)AS ( SELECT 1 UNION ALL SELECT n + 1 FROM n WHERE n< 12 ) SELECT @ RetVal = @ RetVal + Coalesce(DATENAME(MONTH,DATEADD(MONTH,N-1,'01 -Jan-1900'))+',' ,'') FROM n 等于@START_MONTH和@END_MONTH ORDER BY n OPTION(MAXRECURSION 12); 返回左(@RetVal,Len(@RetVal)-1); 结束 创建功能后,使用以下脚本获得所需结果 DECLARE @ TBL TABLE ( STRARTDATE DATETIME , ENDDATE DATETIME ) INSERT INTO @ TBL (STRARTDATE,ENDDATE) SELECT ' 2014年3月8日',' 2014年5月12日 UNION 所有 SELECT ' 15-Aug-2014',' 2014年8月20日' UNION ALL SELECT ' 2014年5月12日',' 2014年8月15日' UNION ALL SELECT ' 01-Jan-2014',' 01-Dec-2014' 选择 *,dbo.FN_GET_DEFORM_MONTHS(DATEPART(m,STRARTDATE),DATEPART(m,ENDDATE)) 来自 @ TBL my input: I have 2 columns start date and end date based on these columns display the another columnStart dateEnd date8-Mar-14 12-May-1415-Aug-1420-Aug-1412-May-1415-Aug-14Output: MOC column should display the Deference between start date and end date, values should be separated by commaStart dateEnd date Moc 8-Mar-1412-May-14 march.april,may15-Aug-1420-Aug-14 august12-May-1415-Aug-14 may,june,july,august 解决方案 The Above solution i have modified here check this :CREATE FUNCTION [dbo].FUN_GETMONTH(@date1 datetime, @date2 datetime)RETURNS VARCHAR(1000)ASBEGINdeclare @tmp varchar(MAX) SET @tmp = '';with cte as (select datename(month,@date1) as [Month_Name],@date1 as datunion allselect datename(month,DateAdd(Month,1,dat)),DateAdd(Month,1,dat) from ctewhere convert(char(6),DateAdd(Month,1,dat),112) <= convert(char(6),@date2,112))select @tmp = @tmp + CASE WHEN [Month_Name]='01' THEN 'JAN' WHEN [Month_Name]='02' THEN 'FEB' WHEN [Month_Name]='03' THEN 'MAR' WHEN [Month_Name]='04' THEN 'APR' WHEN [Month_Name]='05' THEN 'MAY' WHEN [Month_Name]='06' THEN 'JUN' WHEN [Month_Name]='07' THEN 'JUL' WHEN [Month_Name]='08' THEN 'AUG' WHEN [Month_Name]='09' THEN 'SEP' WHEN [Month_Name]='10' THEN 'OCT' WHEN [Month_Name]='11' THEN 'NOV' WHEN [Month_Name]='12' THEN 'DEC' END + ', ' from CTE option (maxrecursion 0) RETURN @tmp;ENDDECLARE @TBL TABLE( STRARTDATE DATETIME, ENDDATE DATETIME)INSERT INTO @TBL(STRARTDATE, ENDDATE)SELECT '2014-01-20', '2014-04-20'UNION ALLSELECT '2014-05-20', '2014-09-20'UNION ALLSELECT '2014-07-20', '2014-07-20'UNION ALLSELECT '2014-06-20', '2014-11-20'select STRARTDATE,ENDDATE, dbo.FUN_GETMONTH(STRARTDATE, ENDDATE) AS MONTHS from @TBLHi,Try this...DECLARE @StartDate DATETIME, @EndDate DATETIME;SELECT @StartDate = '8-Mar-2014' ,@EndDate = '8-May-2014';SELECT DATENAME(MONTH, DATEADD(MONTH, x.number, @StartDate)) AS MonthNameFROM master.dbo.spt_values xWHERE x.type = 'P'AND x.number <= DATEDIFF(MONTH, @StartDate, @EndDate);CheersBelow function will do the trick. First create the below function/****** Object: UserDefinedFunction [dbo].[FN_GET_DEFORM_MONTHS] Script Date: 07/01/2014 12:14:51 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE FUNCTION [dbo].FN_GET_DEFORM_MONTHS(@START_MONTH INT, @END_MONTH INT)RETURNS VARCHAR(1000)ASBEGINDECLARE @RetVal VARCHAR(1000)SELECT @RetVal = '';WITH n(n) AS(SELECT 1UNION ALLSELECT n+1 FROM n WHERE n < 12)SELECT @RetVal=@RetVal + Coalesce(DATENAME(MONTH,DATEADD(MONTH,N-1,'01-Jan-1900'))+ ', ','')FROM nWHERE n BETWEEN @START_MONTH and @END_MONTHORDER BY nOPTION (MAXRECURSION 12); RETURN LEFT(@RetVal, Len(@RetVal)-1);ENDOnce function is created use below script to get desired resultDECLARE @TBL TABLE( STRARTDATE DATETIME, ENDDATE DATETIME)INSERT INTO @TBL(STRARTDATE, ENDDATE)SELECT '08-Mar-2014', '12-May-2014'UNION ALLSELECT '15-Aug-2014', '20-Aug-2014'UNION ALLSELECT '12-May-2014', '15-Aug-2014'UNION ALLSELECT '01-Jan-2014', '01-Dec-2014'select *, dbo.FN_GET_DEFORM_MONTHS(DATEPART(m, STRARTDATE), DATEPART(m, ENDDATE))from @TBL 这篇关于开始日期和结束日期之间的差异以显示另一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 15:43