Getting a specific timezone in rails sql statement

user1010101

In my Rails application I am executing the following query.

sql = "select A.bus_id as busid, A.stop_id as source, A.arrival as atime, B.arrival as dtime from
(SELECT * from schedules as S where S.stop_id = #{startStopId}) A
inner join
(SELECT * from schedules as S where S.stop_id = #{endStopId}) B
on A.bustag = B.bustag
where A.arrival < B.arrival
and A.arrival > #{Time.now} " 

I get the following error

ActiveRecord::StatementInvalid in WelcomeController#create
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '15:21:07 +0000' at line 7: select A.bus_id as busid, A.stop_id as source, A.arrival as atime, B.arrival as dtime from (SELECT * from schedules as S where S.stop_id = 25) A inner join (SELECT * from schedules as S where S.stop_id = 1) B on A.bustag = B.bustag where A.arrival < B.arrival and A.arrival > 2015-05-03 15:21:07 +0000

I basically want to do the following

A.arrival > EASTERN STANDARD TIME.

One thing i noticed is that Time.now seems to be giving me UTC time.

So i thought of this solution

#subtract 4 hours to get eastern standard time from UTC.
@time_ = Time.now -0400

 sql = "select A.bus_id as busid, A.stop_id as source, A.arrival as atime, B.arrival as dtime from
    (SELECT * from schedules as S where S.stop_id = #{startStopId}) A
    inner join
    (SELECT * from schedules as S where S.stop_id = #{endStopId}) B
    on A.bustag = B.bustag
    where A.arrival < B.arrival
    and A.arrival > #{@time_} " 

I am still getting an error. I am not sure what to do anymore. Appreciate all help.

pangpang

You have to use quotes around #{Time.now}.

For example: "...A.arrival > '#{Time.now}' "

If you want change UTC to EST, you can do like this:

Time.now.in_time_zone("EST").to_s

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Momentjs timezone - getting date at time in specific timezone

From Dev

Getting the current Instant in a specific TimeZone

From Dev

Java - Not getting the timestamp for a specific timezone

From Dev

Getting specific date with timezone in Java

From Dev

Rails Handling User specific timezone

From Dev

Getting the current month in a timezone in Rails on Heroku

From Dev

How to convert time to specific timezone in Rails?

From Dev

Rails query to SQL statement

From Dev

Rails query to SQL statement

From Dev

If statement is 1 then run specific SQL statement

From Dev

Finding a Specific Character in SQL Statement

From Dev

Convert SQL Statement to Rails ActiveRecord

From Dev

Getting List of Columns from a SQL Select statement

From Dev

getting multiple values out of an SQL statement

From Dev

Getting error with query statement sql server

From Dev

Getting the definition of the tables and columns via a SQL statement

From Dev

Getting the decriptions of the tables and columns via a sql statement

From Dev

Getting conversion error with SQL Case statement

From Dev

SQL statement in getting unique rows with conditions

From Dev

Restricting WHERE clause to specific SELECT statement in sql

From Dev

Simple SQL statement to retrieve orders on a specific date

From Dev

sql statement does not work on specific table

From Dev

Simple SQL statement to retrieve orders on a specific date

From Dev

Selecting a specific date with a PL/SQL statement

From Dev

SQL SELECT clause with WHERE statement for specific order

From Dev

Rails/SQL statement subtracting a null value

From Dev

Rails ignores constants in SQL SELECT statement

From Dev

how to write Rails 4 scope for SQL statement?

From Dev

Rails sql issue with a simple where statement

Related Related

  1. 1

    Momentjs timezone - getting date at time in specific timezone

  2. 2

    Getting the current Instant in a specific TimeZone

  3. 3

    Java - Not getting the timestamp for a specific timezone

  4. 4

    Getting specific date with timezone in Java

  5. 5

    Rails Handling User specific timezone

  6. 6

    Getting the current month in a timezone in Rails on Heroku

  7. 7

    How to convert time to specific timezone in Rails?

  8. 8

    Rails query to SQL statement

  9. 9

    Rails query to SQL statement

  10. 10

    If statement is 1 then run specific SQL statement

  11. 11

    Finding a Specific Character in SQL Statement

  12. 12

    Convert SQL Statement to Rails ActiveRecord

  13. 13

    Getting List of Columns from a SQL Select statement

  14. 14

    getting multiple values out of an SQL statement

  15. 15

    Getting error with query statement sql server

  16. 16

    Getting the definition of the tables and columns via a SQL statement

  17. 17

    Getting the decriptions of the tables and columns via a sql statement

  18. 18

    Getting conversion error with SQL Case statement

  19. 19

    SQL statement in getting unique rows with conditions

  20. 20

    Restricting WHERE clause to specific SELECT statement in sql

  21. 21

    Simple SQL statement to retrieve orders on a specific date

  22. 22

    sql statement does not work on specific table

  23. 23

    Simple SQL statement to retrieve orders on a specific date

  24. 24

    Selecting a specific date with a PL/SQL statement

  25. 25

    SQL SELECT clause with WHERE statement for specific order

  26. 26

    Rails/SQL statement subtracting a null value

  27. 27

    Rails ignores constants in SQL SELECT statement

  28. 28

    how to write Rails 4 scope for SQL statement?

  29. 29

    Rails sql issue with a simple where statement

HotTag

Archive