Comparing with current time SQL

2019-08-27 15:18发布

I'm trying to get a page to display a list of all the records that are valid at the current time, so comparing the start and end time & date to the time at that current moment. This is the code I have at the moment:

def self.time
find_by_sql("SELECT *
FROM screens s
WHERE (s.starttime < CONVERT(varchar(30), GETDATE(), 114)) AND (s.finishtime > CONVERT(varchar(30), GETDATE(), 114))")
end

However I get this: ActiveRecord::StatementInvalid

I know my code isn't right.. I just don't quite know how to fix it?

1条回答
太酷不给撩
2楼-- · 2019-08-27 16:01
def self.time
  find_by_sql("SELECT *
  FROM screens s
  WHERE (s.starttime < #{Time.now}) AND (s.finishtime >  #{Time.now}")
end
查看更多
登录 后发表回答