相当于 Postgres 中的 strftime [英] Equivalent of strftime in Postgres

查看:28
本文介绍了相当于 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天全站免登陆