如何使用同一表中的现有数据更新 DATETIME 字段
我有一个 sqlite 数据库,当前保存一个名为 Year 的整数字段,当前仅存储年份。在未来的版本中,我想存储完整的日期和时间。
我使用 alter table 更新了表以包含 FullDate 字段。
> ALTER TABLE Files ADD COLUMN UploadDate DATETIME DEFAULT 0;
接下来,我想将所有现有的年份字段迁移到新字段。所以我正在寻找类似的东西:
> UPDATE Files SET UploadDate = (DATETIME('%Y-%m-%d', Year, 1, 1));
不幸的是,这似乎不起作用,因为结果是空的。我还尝试了 date 和 strftime 函数,但它们要么导致数据不正确,要么导致数据为空。
使用同一表中的现有数据更新 DATETIME 字段的正确方法是什么?
I have an sqlite database which currently holds an integer field called Year which currently only stores the year. In future versions I want to store a full date and time.
I updated my table to include a FullDate field using alter table.
> ALTER TABLE Files ADD COLUMN UploadDate DATETIME DEFAULT 0;
Next, I want to migrate all the existing year fields to the new field. So I'm looking for something like:
> UPDATE Files SET UploadDate = (DATETIME('%Y-%m-%d', Year, 1, 1));
Unfortunately this doesn't seem to work as the result is empty. I also tried the date and strftime functions but they either result in incorrect data or empty data.
What's the proper way to update a DATETIME field with existing data in the same table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DATE
和DATETIME
函数没有格式参数。更多信息: http://sqlite.org/lang_datefunc.html
主要问题是 SQLite 不具有任何日期或时间类型,因此您也可以使用以下内容填充字段:
这将执行完全相同的操作。日期不是按类型存储的,但可以根据日期和时间函数进行计算。
The
DATE
andDATETIME
functions don't have a format parameter.For more: http://sqlite.org/lang_datefunc.html
The main catch is that SQLite does not have any date or time types, so that you might as well populate your field with:
And that will do the exact same thing. Dates are not stored as typed, but can be evaluated as such against the date and time functions.