Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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 从用户定义函数中的字符串转换日期和/或时间时,转换失败_Sql_Sql Server - Fatal编程技术网

Sql 从用户定义函数中的字符串转换日期和/或时间时,转换失败

Sql 从用户定义函数中的字符串转换日期和/或时间时,转换失败,sql,sql-server,Sql,Sql Server,处理应返回日期的函数。但我得到了这个错误,我不知道如何修复它 if (@flow_date = eomonth(@flow_date) or day(@flow_date) < day(@lMaturityDate)) begin set @lMaturityDate = eomonth(@lMaturityDate); end else begin

处理应返回日期的函数。但我得到了这个错误,我不知道如何修复它

if (@flow_date = eomonth(@flow_date) or day(@flow_date) < day(@lMaturityDate)) begin
                    set @lMaturityDate = eomonth(@lMaturityDate);
                end

                else begin


                    if  month(@lMaturityDate) < 10 BEGIN
                        set @month=concat('0',month(@lMaturityDate));

                    end
                    else Begin
                        set @month=month(@lMaturityDate);

                    END

                    set @lMaturityDate =convert(date,concat(year(@lMaturityDate),'-',@month,'-',day(@flow_date)))
            end

        END
            return isnull(@lMaturityDate,null);

    END
导致错误的示例代码,我刚刚注意到它抛出的错误超过10个月

declare @month varchar(40);
 declare @lMaturityDate date = '2019-11-31'
 declare @flow_date date = '2019-04-25'

if  month(@lMaturityDate) < 10 BEGIN
                    set @month=concat('0',month(@lMaturityDate));

                end
                else Begin
                    set @month=month(@lMaturityDate);

                END
set @lMaturityDate  =convert(date,concat(year(@lMaturityDate),'-',@month,'-',day(@flow_date)))
select  @lMaturityDate

该函数被破坏,因为它假设flowdate的任何日期也适用于到期日期的月份。显然,28岁以后的任何一天都不是这样

例如,如果Flowdate='2019-06-30'和到期日期='2019-02-28',则结果为日期'2019-02-30',该日期无效

你需要找出你的逻辑来处理这个问题。我建议寻找日期月份+天数>月份1的任何日期。如果是,则使用当月的最后一天


这只是一个建议,你可能还有其他规则。

你有什么建议concatyear@lMaturityDate,“-”,@月,“-”,day@flow_date如果您打印出来,请阅读?向我们展示一些导致错误的示例数据,我得到的错误数月大于10@DaleBurrell请看编辑后的11月只有30天…非常接近。如果您熟悉Oracle的添加月功能。我正试图在SQLServer中做同样的事情。任何简单的代码都会被选择为DATEADDm,1,'2019-10-31',这会意外地导致11月30日。@TomC是的,它在一个方向工作,但在另一个方向不工作。11月30日再加上一个月,你会得到什么?不是12月31日——我认为这是OP的目标。别忘了二月更复杂。同意@SMor。需要业务部门做出决定的事情之一。我倾向于认为一个月的最后一天应该是任何一个月的最后一天,不管有多少天。我相信我的信用卡就是这样做的!