数字“日期” 在数据库中 - 如何使用“正常”与数据库进行交互 日期?

发布于 2024-07-10 04:20:10 字数 392 浏览 8 评论 0原文

我正在使用这个数据库,其中日期列是数值而不是日期值。

是的,我知道我可以通过单击鼠标来更改它,但是使用该数据库的所有应用程序都是由我的一位前任创建的(他之后的每个人都忽略了它并继续构建)。 因此,如果我将其更改为“日期”,很多 af 应用程序都会失败。 :(

嗯,我正在对该数据库进行概述,从一个特定日期到另一个特定日期。我尝试使用下拉列表,但正如您所知,一个列表中的一千个选项非常不方便,甚至丑陋。

我宁愿有小日-月-年的输入字段,但是数据库中的数字日期出现了华尔兹,我必须以某种方式将日期计算回数字值......

一定有一个简单的解决方案,对吗?



我使用 ASP(vbscript) 作为应用程序,它用于内部网络,并且我有一个 Access 数据库。

I'm using this database where the date colomn is a numeric value instead of a Date value.

Yes, I know I can change that with a mouseclick, but all the applications using that database were made by one of my predecessors (and everyone after him just ignored it and built on). So if I'd change it to Date a lot af applications would fail. :(

Well, I am making an overview of that database, ranging from one specfic date to another. I tried using a dropdown list but as you can tell, a thousand options in one list is terribly inconvenient, even ugly.

I rather have small inputfields for day - month - year, but there comes waltzing in the numeric date in the database. I would have to calculate the date back to the numeric value somehow...

There must be an easy solution to this. Right?

I'm using ASP(vbscript) for the application, it's for an intraweb, and I have an Access Database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

变身佩奇 2024-07-17 04:20:10

Access 将为您转换为数字,如前所述,日期存储为数字。

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("TestTable")
rs.AddNew
rs!NumberDate = Now()  'Value stored, eg, 39791.4749074074 '
rs.Update

rs.MoveLast

'To show that it converts back to the correct date / time '
Debug.Print Format(rs!NumberDate, "dd/mm/yyyy hh:nn:ss")

编辑评论:这是一个小测试,显示返回的日期:

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

strFile = "C:\Docs\LTD.mdb"

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
       "Data Source=" & strFile & ";" & _
       "Persist Security Info=False"

strSQL = "SELECT NumberDate FROM TestTable WHERE NumberDate= #2008/12/7#"

rs.Open strSQL, cn, 3, 3
rs.MoveLast

MsgBox rs.RecordCount

Access will convert to a number for you, as was mentioned, the dates are stored as numbers.

Dim rs As DAO.Recordset

Set rs = CurrentDb.OpenRecordset("TestTable")
rs.AddNew
rs!NumberDate = Now()  'Value stored, eg, 39791.4749074074 '
rs.Update

rs.MoveLast

'To show that it converts back to the correct date / time '
Debug.Print Format(rs!NumberDate, "dd/mm/yyyy hh:nn:ss")

EDIT re comment: Here is a small test that shows dates returned:

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

strFile = "C:\Docs\LTD.mdb"

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
       "Data Source=" & strFile & ";" & _
       "Persist Security Info=False"

strSQL = "SELECT NumberDate FROM TestTable WHERE NumberDate= #2008/12/7#"

rs.Open strSQL, cn, 3, 3
rs.MoveLast

MsgBox rs.RecordCount
童话里做英雄 2024-07-17 04:20:10

是的..所以这真的很容易。 我会接受 Remou 的回答, WHERE NumberDate= #2008/12/7# 可以解决问题。

但是,为了结束这个问题,这是我自己摆弄 Functions:

Function DateToNumeric(dayDate)
    DateToNumeric=DateDiff("d","31/12/1899",dayDate) +1 //yup
End Function
       
    response.Write("9/12/2008, should be 39791.<br /><br />")
    response.write("DateToNumeric('9/12/2008') gives: " &DateToNumeric("9/12/2008")& "<br />")
    response.write("CDate('39791') gives: " &CDate(39791)&"<br /><br />")
    response.write("BECAUSE CDate('1') gives: " &CDate(1))

输出的解决方案:

2008 年 9 月 12 日,应该是 39791。

DateToNumeric('9/12/2008') 给出:39791
CDate('39791') 给出:9-12-2008

因为 CDate('1') 给出:31-12-1899

它使得 31/12/1899 不是第 0 天,而是第 1 天。 :/

多谢你们!

Right.. so it was real easy. I'll accept Remou's answer, the WHERE NumberDate= #2008/12/7# does the trick.

But, to put a close to the matter, here's the solution to my own fiddling with Functions:

Function DateToNumeric(dayDate)
    DateToNumeric=DateDiff("d","31/12/1899",dayDate) +1 //yup
End Function
       
    response.Write("9/12/2008, should be 39791.<br /><br />")
    response.write("DateToNumeric('9/12/2008') gives: " &DateToNumeric("9/12/2008")& "<br />")
    response.write("CDate('39791') gives: " &CDate(39791)&"<br /><br />")
    response.write("BECAUSE CDate('1') gives: " &CDate(1))

output:

9/12/2008, should be 39791.

DateToNumeric('9/12/2008') gives: 39791
CDate('39791') gives: 9-12-2008

BECAUSE CDate('1') gives: 31-12-1899

It's made it so that 31/12/1899 is not day Zero, but day One. :/

Thanks guys!

心的憧憬 2024-07-17 04:20:10

Access 在内部将日期存储为浮点数(自 1899 年 12 月 31 日以来的天数或其他),您是否尝试过使用 CDate() 将数字转换回日期? 然后您应该能够使用 BETWEEN 进行查询。

另一种可能性是日期以数字形式存储但未转换(即 31121899),在这种情况下,您应该能够使用 Format() 或 Day() 或 Month() 或 Year() 获取适当的日期部分。

希望这可以帮助。

Access stores date internally as a floating point number (number of days since 31.12.1899 or something), have you tried using CDate() to convert the number back to a date? You should be able to query using BETWEEN then.

Another possibility is that the date is stored numerically but not converted (i.e 31121899), in this case you should be able to get the appropriate date parts using either Format() or Day() or Month() or Year().

Hope this helps.

清君侧 2024-07-17 04:20:10

遗憾的是,日期存储为 39791 -> 2008 年 12 月 9 日。 浮点数。

我有一个 javascript 函数(也是由同一前辈编写的),可以将日期转换为可读格式。 这是相反的方式,回到那个让我困惑的数值的方式。

Sadly the date is stored as 39791 -> 9-dec-2008. The floating point number.

I have a javascript function (also written by the same predecessor) that convertes the date to a readable format. It's the other way around, the way back to that numeric value that baffles me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文