sql - 如何删除时间戳等于 2018 的记录
问题描述
我有一个带有 table 的 Postgres 数据库absences
。我正在尝试删除时间戳中包含“2018”的所有记录,如下所示:
DELETE FROM absences WHERE date LIKE '%2018%';
但我收到此错误消息:
absence-registrator_production=> DELETE FROM absences WHERE date LIKE '2018%';
ERROR: operator does not exist: timestamp without time zone ~~ unknown
LINE 1: DELETE FROM absences WHERE date LIKE '2018%';
有谁知道如何从缺勤表中删除日期字段中包含“2018”的记录?
解决方案
另一种选择是使用可能使用该列上的索引的条件:
where date >= timestamp '2018-01-01 00:00:00'
and date < timestamp '2019-01-01 00:00:00';
如果您不想写一个长的“时间戳文字”并且只是将一个数字传递给查询,您可以使用make_timestamp()
where date >= make_timestamp(2018,1,1,0,0,0)
and date < make_timestamp(2019,1,1,0,0,0);
如果该值是您可以使用的参数:
where date >= make_timestamp(?,1,1,0,0,0)
and date < make_timestamp(? + 1,1,1,0,0,0);
提取功能也可以工作:
where extract(year from date)::int = 2018
或使用 to_char()
where to_char(date, 'yyyy') = '2018'
推荐阅读
- wordpress - 如何在 WordPress 中的评论旁边添加用户角色标签
- bash - Bash:通过for-in-loop执行文件
- angular6 - NGXS - 使用 redux devtools 插件/记录器的 Angular 6 过滤表单操作
- excel - 如果单元格值等于:“失败”,则关闭时 Excel 中的 VBA 代码消息框
- ios - 在哪里检查“如果我使用 v4.29.0 或更高版本的 SDK,则需要添加:”
- apache-kafka - 关于 Kafka 中的分区和解决方案架构的说明
- r - 使用新数据框从表中预测线性模型
- java - 如何修复在类路径中找不到类
- angular - 在Angular4中自动将一个页面导航到另一个页面
- java - 使用 JPA 获取数据库用户并在 Entity Attribute 中设置