Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从一对年/月值中减去4个月_Sql_Sql Server_Sql Server 2008_Sql Server 2005 - Fatal编程技术网

Sql 从一对年/月值中减去4个月

Sql 从一对年/月值中减去4个月,sql,sql-server,sql-server-2008,sql-server-2005,Sql,Sql Server,Sql Server 2008,Sql Server 2005,我想减去4个月,期间定义为年和月: UPDATE [MAS_YCA].[dbo].[temp_AR_SalesPersonhistory] SET FiscalYear = year(DATEADD(month,-4,DATEADD(DAY,-1,DATEADD(month,cast(FiscalPeriod as Int),DATEADD(year,cast(FiscalYear as Int)-1900,0))))), FiscalPeriod = right('00'+cast(month

我想减去4个月,期间定义为年和月:

UPDATE [MAS_YCA].[dbo].[temp_AR_SalesPersonhistory]
SET FiscalYear = year(DATEADD(month,-4,DATEADD(DAY,-1,DATEADD(month,cast(FiscalPeriod as Int),DATEADD(year,cast(FiscalYear as Int)-1900,0))))),
FiscalPeriod = right('00'+cast(month(DATEADD(month,-4,DATEADD(DAY,-1,DATEADD(month,cast(FiscalPeriod as Int),DATEADD(year,cast(FiscalYear as Int)-1900,0))))) as varchar),02)
GO
我得到的错误是
向datetime列添加值导致溢出。


“会计年度”和“期间”字段在数据表中都定义为varchar。

听起来您需要检查“财政年度”和“财政期间”列中的数据。最可能的情况是,“会计年度”列中的年度无效

SQL Server中的日期范围是1753年1月1日到9999年12月31日。所以在这之外的任何一年都会导致你的错误

易于检查(编辑、添加空字符串和空字符串):

从[MAS_YCA].[dbo].[temp_AR_salersonhistory]中选择*
铸造(内部为财政部)大于9999或铸造(内部为财政部)小于1753
或FiscalYear为NULL或FiscalYear=''

fiscalyear和fiscalperiod列中的数据是什么?您需要显示一些示例数据,因为还不清楚
fiscalyear
fiscalperiod
是什么样子。您说过它们是年和月
varchar
值,但前导零之类的细节可能是个问题。为什么要将整数存储为
varchar
值?它没有返回任何行。已更新。还要检查空字符串和空字符串。但我的观点是,该列(或者可能是月份列,但不太可能)中的某些内容超出了SQL Server的日期范围,所以请开始查找它。
Select * from [MAS_YCA].[dbo].[temp_AR_SalesPersonhistory]
where cast(FiscalYear as Int) > 9999 or cast(FiscalYear as Int) < 1753
or FiscalYear is NULL or FiscalYear = ''