转换失败错误 SQL 日期格式与 C# 数据表日期时间格式不匹配,具体取决于系统日历设置

Conversion failed Error SQL Date format Mismatch with C# Datatable datetime fromat depending on system calender setting

我们在 SQL 服务器中有一个存储过程,它期望日期时间列采用 MMddyyyy 格式并导致错误

Conversion failed when converting date and/or time from character string.

只要系统日历设置为 ddMMyyyy 格式。如果为系统设置了其他日期格式,例如 dd-MMM-yyyy 工作正常并且 returns 没有错误,则相同的 SP 工作正常。当日历设置更改为 ddMMyyyy 格式时,SP 会抛出错误。它适用于 12 以下的日期,因为它将第一位数字作为月份,但在给出更高的日期(如 15 或 28)时会失败。我正在尝试找到一种解决方案,以便它适用于所有日历设置,因为我无法限制最终用户更改他们的日历设置。请帮我 ... 这是我的SP

CREATE PROCEDURE [dbo].[usp_704000] 
    -- Add the parameters for the stored procedure here
    @Dt uttTrnBRS readonly,
    @Dt1 uttTrnBRS1 readonly
AS
BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    -- Insert statements for procedure here
    Declare @RowStatus Int
    Declare @TrnId Int
    Declare @DocNo Varchar(15)
    Declare @NextNo Int
    Declare @Return SmallInt

    Select  @RowStatus = MAX(RowStatus), @Return = 0, @TrnId = 0
    From    @Dt T0  

    BEGIN TRANSACTION
        --NEW Mode Handle the Insertion of New Records
        IF (@RowStatus = 2)
            BEGIN
                BEGIN TRY
                    Insert into TrnBRS (CmpId, FinYear, FYPrd, DocId, SeriesId, DocDate, DocRef, DocStatus, BankCode, BankName, BankAcNo, AcId, AcCode,
                                        AcDesc, BankAcId, EndDt, EndBalAmt, LastRcnDt, BalAmt, Diff, Remarks, BranchCode, BaseId, CUserId, CreateDt)
                    Select  T0.CmpId, T2.FinYear, T2.FYPrdId FYPrd, T3.DocId, T0.SeriesId, Cast(T0.DocDate as Date) DocDate, T0.DocRef, '3' DocStatus, 
                            T0.BankCode, T0.BankName, T0.BankAcNo, T0.AcId, T0.AcCode, T0.AcDesc, T0.BankAcId,
                            Cast(T0.EndDt as Date) EndDt, ISNULL(T0.EndBalAmt, 0) EndBalAmt, Cast(T0.LastRcnDt as Date) LastRcnDt, ISNULL(T0.BalAmt, 0) BalAmt, ISNULL(T0.Diff, 0) Diff,
                            T0.Remarks, T3.BranchCode, T3.BaseId, T0.CUserId, GetDate() CreateDt
                    From    @Dt T0  Cross Join CmpMst T1 
                                    Left Join FYPrdMst T2 ON T0.DocDate Between T2.PrdFrom and T2.PrdTo
                                    Inner Join (Select  T0.DocId, T0.BaseId, T1.SeriesId, T1.SFinYear, T1.EFinYear, T0.BranchCode 
                                                From    DocNum T0 Inner Join DocNum1 T1 ON T0.DocId = T1.DocId
                                                Where   T0.BaseId = 34) T3 ON T2.FinYear Between T3.SFinYear and ISNULL(T3.EFinYear, 3000)
                    Where   T0.RowStatus = 2;
                    IF (@@ERROR = 0) 
                        SET @TrnId = @@IDENTITY;

                    IF (@@ERROR = 0)  
                        Insert into TrnBRS1 (TrnId, LineNum, BranchCode, PDocNo, PDocDate, PDocRef, PRefDate, DrAmtLC, CrAmtLC, Narration, ClearAmt, BankDt, PTrnId, PBaseId, PDocId,
                                             JnlBaseId, JnlDocId, JnlDocNo, JnlTrnId, JnlLineNum, AdjEntry, Notes, RowIndex)
                        Select  @TrnId TrnId, ROW_NUMBER() OVER (Order by T0.RowIndex) LineNum, T0.BranchCode, T0.PDocNo, Cast(T0.PDocDate as Date) PDocDate, T0.PDocRef,
                                Cast(T0.PRefDate as Date) PRefDate, T0.DrAmtLC, T0.CrAmtLC, T0.Narration, T0.ClearAmt, Cast(T0.BankDt as Date) BankDt, T0.PTrnId, T0.PBaseId, T0.PDocId,
                                T0.JnlBaseId, T0.JnlDocId, T0.JnlDocNo, T0.JnlTrnId, T0.JnlLineNum, T0.AdjEntry, T0.Notes, ROW_NUMBER() OVER (Order by T0.RowIndex) RowIndex
                        From    @Dt1 T0 
                        Where   ISNULL(T0.IsCheck, 'N') = 'Y'; -- and ISDATE(CONVERT(Varchar(10), T0.BankDt, 111)) = 1;

                    IF (@@ERROR = 0)  
                        Insert into TrnBRS2 (TrnId, LineNum, BranchCode, PDocNo, PDocDate, PDocRef, PRefDate, DrAmtLC, CrAmtLC, Narration, PTrnId, PBaseId, PDocId,
                                             JnlBaseId, JnlDocId, JnlDocNo, JnlTrnId, JnlLineNum, Notes, RowIndex)
                        Select  @TrnId TrnId, ROW_NUMBER() OVER (Order by T0.RowIndex) LineNum, T0.BranchCode, T0.PDocNo, T0.PDocDate, T0.PDocRef,
                                T0.PRefDate, T0.DrAmtLC, T0.CrAmtLC, T0.Narration, T0.PTrnId, T0.PBaseId, T0.PDocId,
                                T0.JnlBaseId, T0.JnlDocId, T0.JnlDocNo, T0.JnlTrnId, T0.JnlLineNum, T0.Notes, ROW_NUMBER() OVER (Order by T0.RowIndex) RowIndex
                        From    @Dt1 T0 
                        Where   ISNULL(T0.IsCheck, 'N') = 'N'; -- and ISDATE(CONVERT(Varchar(10), T0.BankDt, 111)) = 1;

                    IF (@@ERROR = 0)  
                        Update  TrnJnl1 
                        SET     RecnDt = T1.BankDt, 
                                RecnSys = 'Y', RecnTrnId = @TrnId, RecnType = 1
                        From    TrnJnl1 T0 Inner Join TrnBRS1 T1 ON T0.TrnId = T1.JnlTrnId and T0.LineNum = T1.JnlLineNum and T1.TrnId = @TrnId;

                    IF (@@ERROR = 0)  
                        Select  @NextNo = T1.NextNo + 1, 
                                @DocNo = ISNULL(T1.Prefix, '') + REPLICATE('0', T1.NumLen-LEN(CAST(@NextNo as VARCHAR(5))))+CAST(@NextNo as VARCHAR(5)) +
                                         ISNULL(T1.Suffix, '')
                        From    TrnBRS T0   Inner Join DocNum1 T1 ON T0.DocId = T1.DocId and T0.SeriesId = T1.SeriesId
                        Where   T0.TrnId = @TrnId

                    IF (@@ERROR = 0)  
                        Insert into BankMst3 (BankAcId, BRDate, AcId, BRAmtLC, CUserId, CreateDate)
                        Select  T0.BankAcId, T0.EndDt, T0.AcId, T0.EndBalAmt, T0.CUserId, GetDate() CreateDate
                        From    TrnBRS T0
                        Where   T0.TrnId = @TrnId;

                    IF (@@ERROR = 0)                    
                        Update  DocNum1 
                        SET     NextNo = @NextNo 
                        From    DocNum1 T0  Inner Join TrnBRS T1 ON T0.DocID = T1.DocID and T0.SeriesId = T1.SeriesId and T1.TrnId = @TrnId 

                    IF (@@ERROR = 0)  
                        Update  TrnBRS SET DocNo = @DocNo Where TrnId = @TrnId
    
                    IF (@@ERROR = 0) 
                        BEGIN
                            COMMIT TRANSACTION;                     
                            Select 0 RetVal, @TrnId TrnId, 'Transaction Saved...' ErrMsg, 0 ErrType;
                            RETURN
                        END
                END TRY
                BEGIN CATCH
                    BEGIN
                        ROLLBACK TRANSACTION;
                        SELECT  -1 RetVal, @TrnId TrnId, ERROR_MESSAGE() AS ErrMsg, 1 ErrType
                        RETURN
                    END
                END CATCH
            END

        ELSE
            BEGIN
                BEGIN TRY
                    Insert into TrnBRS (CmpId, FinYear, FYPrd, DocId, SeriesId, DocDate, DocRef, DocStatus, CancelState, CanclTrnId, EndDt, EndBalAmt,
                                        LastRcnDt, BalAmt, Diff, Remarks, BaseId, CUserId, CreateDt)
                    Select  T0.CmpId, T0.FinYear, T0.FYPrd, T0.DocId, T0.SeriesId, T0.DocDate, T0.DocRef, '3' DocStatus, 'N' CancelState, T0.TrnId CanclTrnId,                       
                            T0.EndDt, ISNULL(T0.EndBalAmt, 0) EndBalAmt, T0.LastRcnDt, ISNULL(T0.BalAmt, 0) BalAmt, ISNULL(T0.Diff, 0) Diff,
                            T0.Remarks, T0.BaseId, T0.CUserId, GetDate() CreateDt
                    From    TrnBRS T0 Inner Join @Dt T1 ON T0.TrnId = T1.TrnId 
                    Where   T1.RowStatus = 3;

                    SET @TrnId = @@IDENTITY;
            
                    Update  TrnBRS 
                    SET     CancelState = 'Y', DocStatus = '4', CanclTrnId = @TrnId
                    From    TrnBRS T0 Inner Join @Dt T1 ON T0.TrnId = T1.TrnId and T1.RowStatus = 3;

                    Insert into TrnBRS1 (TrnId, LineNum, BranchCode, PDocNo, PDocDate, PDocRef, PRefDate, DrAmtLC, CrAmtLC, Narration, ClearAmt,
                                         BankDt, PTrnId, PBaseId, PDocId, JnlBaseId, JnlDocId, JnlDocNo, JnlTrnId, JnlLineNum, AdjEntry, Notes, RowIndex)
                    Select  @TrnId TrnId, T0.LineNum, T0.BranchCode, T0.PDocNo, T0.PDocDate, T0.PDocRef, T0.PRefDate, T0.DrAmtLC, T0.CrAmtLC,
                            T0.Narration, T0.ClearAmt *-1, T0.BankDt, T0.PTrnId, T0.PBaseId, T0.PDocId, T0.JnlBaseId, T0.JnlDocId, T0.JnlDocNo,
                            T0.JnlTrnId, T0.JnlLineNum, T0.AdjEntry, T0.Notes, T0.RowIndex
                    From    TrnBRS1 T0 Inner Join @Dt T1 ON T0.TrnId = T1.TrnId and T1.RowStatus = 3;

                    Update  TrnJnl1 
                    SET     RecnDt = NULL, RecnSys = NULL, RecnTrnId = NULL, RecnType = NULL 
                    From    TrnJnl1 T0 Inner Join TrnBRS1 T1 ON T0.TrnId = T1.JnlTrnId and T0.LineNum = T1.JnlLineNum and T1.TrnId = @TrnId     

                    Select  @NextNo = T1.NextNo + 1, 
                            @DocNo = ISNULL(T1.Prefix, '') + 
                                        Substring(Cast(Year(T0.DocDate) as VARCHAR(4)),3,2) + REPLICATE('0', T1.NumLen-LEN(CAST(@NextNo as VARCHAR(5))))+CAST(@NextNo as VARCHAR(5)) +
                                        ISNULL(T1.Suffix, '')
                    From    @Dt T0  Inner Join DocNum1 T1 ON T0.DocId = T1.DocId
                            
                    Update  DocNum1 
                    SET     NextNo = @NextNo 
                    From    DocNum1 T0  Inner Join @Dt T1 ON T0.DocID = T1.DocID 
                    IF (@@ERROR <> 0) RETURN @@ERROR

                    Update  TrnBRS SET DocNo = @DocNo Where TrnId = @TrnId
                    IF (@@ERROR <> 0) RETURN @@ERROR

                    BEGIN
                        COMMIT TRANSACTION;
                        Select 0 RetVal, @TrnId TrnId, 'Transaction Reversed...' ErrMsg, 0 ErrType;
                        RETURN
                    END
                END TRY
                BEGIN CATCH
                    BEGIN
                        ROLLBACK TRANSACTION
                        SELECT  -1 RetVal, @TrnId TrnId, ERROR_MESSAGE() AS ErrMsg, 1 ErrType
                        RETURN
                    END
                END CATCH
            END
END

我曾尝试使用转换语句 SELECT CONVERT(DATE, T1.BankDt, 103) 而不是 T1.BankDt(这是我们试图操作的日期时间字段)来转换 ddMMyyyy 格式 (103),但没有成功。它仍然会抛出错误,指出 12 以上的日期转换失败,因为它期望 MMddyyyy 格式的日期

编辑 2

谢谢大家的支持... 我终于找到了问题,真正的问题出在另一个存储过程中,该存储过程用于填充初始值,其中 BankDt 字段填充了 '' 空字符串,以便该字段被视为字符串而不是日期字段。我已将 '' BankDt 更改为 Convert(Date,null,103) BankDt ...现在一切正常

真正的问题出在另一个用于填充初始值的存储过程中,其中 BankDt 字段填充了 '' 空字符串,以便该字段被视为字符串而不是日期字段。我变了

'' BankDt

Convert(Date,null,103) BankDt

...现在一切正常