简体   繁体   English

将以文本形式存储的长日期转换为短日期Access SQL

[英]Convert long date stored as text to short date Access SQL

I am trying to convert a long date stored as text to a short date via MS Access SQL. 我试图通过MS Access SQL将以文本形式存储的长日期转换为短日期。

For example I have a table which parsed information from a website and one of the field is Tuesday, June 17, 2014. I want to run an update query in another table which takes this value and converts it to 17/06/2014. 例如,我有一个分析网站信息的表,其中一个字段是2014年6月17日,星期二。我想在另一个表中运行更新查询,该表将使用此值并将其转换为17/06/2014。

Any help on what functions I can use please? 请问我可以使用哪些功能?

thanks 谢谢

Elton 埃尔顿

For date strings like "Tuesday, June 17, 2014" a VBA function like this 对于诸如“ 2014年6月17日星期二”之类的日期字符串,VBA函数是这样的

Option Compare Database
Option Explicit

Public Function ParseDateString(DateString As Variant) As Variant
    If IsNull(DateString) Then
        ParseDateString = Null
    Else
        ParseDateString = CDate(Split(DateString, ", ", 2)(1))
    End If
End Function

will convert the string to a true Date value. 将字符串转换为真实的Date值。 If you are running an update query and putting the resulting value into a Date/Time field in the table then you DO NOT want to convert the date to dd/mm/yyyy format. 如果你正在运行一个更新查询,并把所得到的值转换成Date/Time字段,则表中你希望将日期转换dd/mm/yyyy格式。 Just use the result of the function (the true Date value). 只需使用函数的结果(真实的Date值)即可。

If you must convert the date to a string then use the unambiguous date format yyyy-mm-dd . 如果必须将日期转换为字符串,请使用明确的日期格式yyyy-mm-dd If you convert to dd/mm/yyyy format, Access might mangle ambiguous dates and 12/06/2014 could be interpreted as December 6, not June 12. 如果转换为dd/mm/yyyy格式,Access可能会混淆不明确的日期,并且12/06/2014可以解释为12月6日, 而不是 6月12日。

Building on @VBlades' example, but allowing the year to be different from 2014, assuming that other years might appear in the source data. 以@VBlades的示例为基础,但假设其他年份可能出现在源数据中,则允许该年份与2014年不同。

This will only work if the dates you wish to parse are formated consistently. 仅当您要解析的日期格式一致时,此方法才有效。

Paste this function into a vba module and call it from your query. 将此函数粘贴到vba模块中,然后从查询中调用它。

Function dateParser(datestr As String) As Variant
Dim day_month, year, day_month_year As String
day_month = Split(datestr, ",")(1) 
year = Split(datestr, ",")(2)
day_month_year = day_month + ", " + year 
dateParser = Format(day_month_year, "dd/mm/yyyy")  
End Function

尝试:

Format(Split("Tuesday, June 17, 2014", ",")(1), "dd/mm/yyyy")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM