[Solved] rails postgres ERROR: invalid input syntax for type double precision

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.

Solution #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’.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.