sql - Rails postgres错误: invalid input syntax for type double precision

标签 sql ruby-on-rails postgresql

我正在尝试以下操作:

user_calendars.where("extract(day from time_from) = ? ", next_day)

但我不断收到此错误:

PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type double precision: "Saturday" LINE 1: ..."user_id" = $1 AND (extract(day from time_from) = 'Saturday'...

不知道为什么它指出类型为 double。

最佳答案

在 PostgreSQL 中,表达式 extract(day from time_from) 返回一个 double 类型的数字,表示一个月中的第几天。 星期六显然不是有效的 double 。

如果您需要 where() 的参数来匹配字符串“Saturday”(匹配星期几),请使用 to_char() 函数.

user_calendars.where("trim(to_char(time_from, 'Day')) = ? ", next_day)

您需要 trim(),因为这种对 to_char() 的调用会填充到 9 个字符。

对于参数“Day”来说,大小写很重要。如果您将其键入为“day”,则返回值将与“Saturday”不匹配。相反,像 to_char(time_from, 'day') 这样的表达式将返回类似“saturday”的内容。

关于sql - Rails postgres错误: invalid input syntax for type double precision,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203836/

相关文章:

ruby-on-rails - 从 Active Record 范围内访问类方法?

sql - 更新 postgresql 中的列

sql - 聚合还是不聚合,这是数据库模式设计的问题

ruby-on-rails - Gem .gemspec 中的本地依赖

ruby-on-rails - 使用 PaperClip AV 上传后视频显示黑屏

python - 我该如何解决这个问题 : ERROR: Command errored out with exit status 1 : when trying to "pip install psycopg2"

Mysql删除重复的记录,除了一个有最大时间

sql - mysql 在大型数据集或循环中的位置?

python - 为什么 Odoo 删除日期时间字段中的毫秒数?

ruby-on-rails - Rails 看不到我的 Postgres 数据库