你可以使用 PostgreSQL 内置函数 substring()position() 来从日期字符串中获取年份。具体步骤如下:

  1. 使用 position() 函数获取字符串中 - 符号的位置。
SELECT position('-' in '2022-10'); -- 返回 5
  1. 使用 substring() 函数从字符串中截取年份部分。
SELECT substring('2022-10' from 1 for position('-' in '2022-10')-1); -- 返回 2022

综合起来,你可以这样获取年份:

SELECT substring(字段名 from 1 for position('-' in 字段名)-1) FROM 表名;

其中,字段名 是你要获取年份的日期字符串字段名,表名 是该字段所在的表名。

postgresql怎么从日期字符串中获取年份如 数据库字段值是varchar类型 的2022-10怎么获取年份 2022 ?

原文地址: https://www.cveoy.top/t/topic/Mc0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录