【问题标题】:How to use substring function in postgresql?如何在 postgresql 中使用子字符串函数?
【发布时间】:2021-07-27 12:53:01
【问题描述】:

我正在尝试在 postgresql 中使用子字符串函数,但错误是

'没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。'

这是代码

select order_id, ship_date,
Substring (ship_date from 5 for 2) as month
from tewt
order by ship_date;

对此的任何建议都会有所帮助。

【问题讨论】:

  • 为什么不使用日期函数来提取月份?
  • 样本数据、所需结果以及解释ship_date 的类型都会有所帮助。
  • 当您在 Stackoverflow 上提问时,请添加您的示例数据并解释您想要的结果并添加您的预期结果。

标签: sql postgresql substring


【解决方案1】:

如果ship_date 是日期,则不要使用字符串函数。

从日期中提取月份的正确方法是使用extract()

select order_id, ship_date,
       extract(month from ship_date) as month
from tewt
order by ship_date;

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2021-09-11
      • 2012-05-18
      • 2022-06-21
      • 2019-07-18
      • 2019-10-26
      • 2015-04-02
      • 2018-10-16
      • 2012-04-05
      • 1970-01-01
      相关资源
      最近更新 更多