【问题标题】:MS Access - Converting Date Format for Short TextMS Access - 将日期格式转换为短文本
【发布时间】:2019-05-08 02:15:17
【问题描述】:

我有一个带有表 Door Activity Log 的 .mdb Access 数据库。其中有一个名为Door Activity Log Date 的列,类型为Short Text。目前,其格式为美国日期格式(mm/dd/yyyy)。

如何在 Access 中转换/操作日期(我猜短文本只是字符串),使其变为 dd/mm/yyyy

【问题讨论】:

  • 可以使用 Format() 函数。 Format([Door Activity Log Date], "dd/mm/yyyy")。结果是一个字符串,而不是一个真实的日期。实际上并没有改变价值的存储方式。评论allenbrowne.com/ser-36.html
  • 你是什么意思它不会改变值的存储方式?这不是 Date/Time 列,而是 Short Text 。实际上,我正在尝试将表从 Access 导入到 SQL Server 中的相同表中。只有 SQL Server 中的那个在 dd/mm/yyyy 中。
  • 不管字段类型是什么。格式化不会更改 Access 中的原始存储值。那可能是一个无关紧要的评论。保存到 SQLServer 的值将是 Format() 计算结果的字符串。

标签: ms-access ms-access-2013


【解决方案1】:

我用 VBA 解决了

Private Sub dateFormattingConvert()

Dim rs As DAO.Recordset
Dim oldDate As String
Dim newDate As String
Dim year As String
Dim month As String
Dim day As String
Dim fullString As String
Set rs = CurrentDb.OpenRecordset("Door Activity Log")

If Not (rs.EOF And rs.BOF) Then
    rs.MoveFirst
    Do Until rs.EOF = True
        Debug.Print (rs![Door Activity Log Date])
        oldDate = rs![Door Activity Log Date]
        year = Right(oldDate, 4)
        month = Left(oldDate, 2)
        day = Mid(oldDate, 4, 2)
        fullString = day & "/" & month & "/" & year

        rs.Edit
        rs![Door Activity Log Date] = fullString
        rs.Update

        rs.MoveNext
    Loop
Else
    MsgBox "No more records."
End If

MsgBox "Finished"

rs.Close
Set rs = Nothing

End Sub

【讨论】:

    【解决方案2】:

    日期应始终存储为日期,而不是文本。

    所以,在表中创建一个新字段,ActivityLogDate,并用如下查询填充它:

    Update 
        [Door Activity Log]
    Set 
        ActivityLogDate = DateSerial(Mid([Activity Log Date], 7, 4), Mid([Activity Log Date], 1, 2), Mid([Activity Log Date], 4, 2))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多