在Postgres中等效于strftime [英] Equivalent of strftime in Postgres

查看:154
本文介绍了在Postgres中等效于strftime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我的SQLite数据库迁移到开发服务器上的Postgres之后,我得到了

After migrating my SQLite database to Postgres on the development server, I get the

PG::UndefinedFunction: ERROR:  function strftime(unknown, date) does not exist

是否有一个我可以用来保持其功能的函数我的应用程式?

Is there a function I could use to keep the functionality across my app?

编辑:尝试使用to_char但失败了。特别是,我不明白如何在诸如

tried using to_char but failed miserably. In particular, I do not understand how to implement it in a construction such as

def self.query_by_year_month(y, m)
  where("strftime('%Y', date) = ? and strftime('%m', date) = ?", y, m)
end

谢谢!

推荐答案

看起来像它是学习SQL的绝佳时机!使用以下代码使我的方法生效:

Looks like it is a great time to start learning SQL! Made my method work using the following code:

  def self.query_by_year_month(y, m)
    where("extract(year from date) = ? and extract(month from date) = ?", y, m)
  end

这篇关于在Postgres中等效于strftime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆