代码之家  ›  专栏  ›  技术社区  ›  Andre

如何从日期变量中删除秒的分数?

  •  0
  • Andre  · 技术社区  · 6 年前

    我读了报纸 pwdLastSet DATETIME2(0) SQL Server表中的列。我不想存储几秒钟。

    这是一个8字节的整数,从1601年1月1日凌晨12:00开始以100纳秒的步长表示 this function 将其转换为日期变量。

    这一点继续以失败告终 ODBC-call failed ,我花了相当长的时间才发现是该函数返回的额外精度导致了错误。

    问题:

    最好的方法是什么,以消除分数秒从一个 Date 变量



    在SQL Server中:

    CREATE TABLE TestDT (
        ID      INT             NOT NULL,
        colDT2  DATETIME2(0)    NULL,
    
        CONSTRAINT PK_TestDT PRIMARY KEY (ID)
    )
    GO
    INSERT TestDT (ID) VALUES (1)
    GO
    

    Microsoft ODBC Driver 17 for SQL Server . 默认的“SQL Server”驱动程序实际上不知道如何使用DATETIME2。

    在Access VBA中:

    Public Sub TestDT()
    
        Dim DB As DAO.Database
        Dim RS As DAO.Recordset
        Dim dte As Date
        Dim i As Long
        
        Set DB = CurrentDb
        
        ' Random date+time
        dte = CDate("2018-12-24 15:16:17")
        
        ' 1st iteration: write original date+time -> works
        ' 2nd iteration: try to write date+time with fractional seconds -> error for DATETIME2(0) column
        
        For i = 1 To 2
            If i = 2 Then
                ' Introduce milliseconds nastiness
                dte = dte + 0.00001
            End If
            Debug.Print "Iteration " & i, Format(dte, "yyyy-mm-dd hh:nn:ss")
                
            Set RS = DB.OpenRecordset("SELECT * FROM TestDT WHERE ID = 1", dbOpenDynaset)
        
            With RS
                .Edit
                !colDT2 = dte
                On Error Resume Next
                .Update
                
                If Err.Number <> 0 Then
                    Debug.Print "Error " & Err.Number, Err.Description
                    ' The DAO Errors collection shows the actual error
                    Debug.Print Errors(0).Description
                Else
                    Debug.Print "Update OK"
                End If
                On Error GoTo 0
                
                .Close
            End With
        Next i
    
    End Sub
    

    输出:

    Iteration 1   2018-12-24 15:16:17
    Update OK
    Iteration 2   2018-12-24 15:16:18
    Error 3146    ODBC-Aufruf fehlgeschlagen.
    [Microsoft][ODBC Driver 17 for SQL Server]Datetime field overflow. 
        Fractional second precision exceeds the scale specified in the parameter binding.
    
    3 回复  |  直到 4 年前
        1
  •  4
  •   Gustav    6 年前

    您可以通过以下方式四舍五入到第二个:

    PwdLastSetSecond = CDate(Int(PwdLastSet * 86400) / 86400)
    
        2
  •  1
  •   Albert D. Kallal    6 年前

    日期值(日期/时间表达式)

    因此,你可以得到:

    清除时间部分的正确功能是:

    时间值(日期/时间表达式)

    或时间值(任何日期/时间表达式)

    格式没有.ms这样的东西。

    这将给你一个月以上的时间。

    2018-12-24 15:16:17.1217

    在上图中,te 1217是12个月17秒。。

    格式上没有“ms”这样的东西。这就是为什么你会看到溢出 .

    您只能在访问中获得秒数。

    只需使用标准格式命令。如果您想去掉“额外”时间:

      Set rst = CurrentDb.OpenRecordset("dbo_TimeTest2", dbOpenDynaset, dbSeeChanges)
    
      rst.Edit
      rst!StartTimeD = Format(rst!StartTimeD, "MM-DD-YYYY hh:nn:ss")
      rst.Update
    

        3
  •  0
  •   Andre    6 年前

    我想出了

    dte = CDate(Int(dte) + TimeSerial(Hour(dte), Minute(dte), Second(dte)))
    

    但这相当笨拙(