rails postgres ERROR: invalid input syntax for type double precision

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

i'm trying the following:

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

But i keep getting this Error:

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

Not sure why it points out type double.

回答1:

In PostgreSQL, the expression extract(day from time_from) returns a number of type double, representing the day of the month. Saturday is clearly not a valid double.

If you need the argument to where() to match the string 'Saturday' (to match the day of the week), then use the to_char() function.

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

You need trim(), because this kind of call to to_char() is padded to 9 characters.

Case is significant for the argument 'Day'. If you key it as 'day', the returned value won't match 'Saturday'. Instead, an expression like to_char(time_from, 'day') will return something like 'saturday'.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!