【问题标题】:rails postgres ERROR: invalid input syntax for type double precisionrails postgres 错误:双精度类型的输入语法无效
【发布时间】:2014-11-29 23:49:20
【问题描述】:

我正在尝试以下方法:

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。

【问题讨论】:

    标签: sql ruby-on-rails postgresql


    【解决方案1】:

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

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

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

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

    大小写对于论点“日”很重要。如果将其键入为“day”,则返回的值将与“Saturday”不匹配。相反,to_char(time_from, 'day') 之类的表达式将返回 'saturday' 之类的内容。

    【讨论】:

    • 谢谢@Mike。就是这样。
    • 或者将 Ruby next_day 字符串转换为与 extract(day ...) 返回的数字匹配的数字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2012-12-24
    相关资源
    最近更新 更多