当前位置:   article > 正文

PostgreSQL获取指定范围的年份,月份_pgsql 获取当年

pgsql 获取当年

生成近8天的日期(now() 和“ interval '8 DAY' ” 可以修改成指定日期来生成)格式 yyyy-mm-dd

  1. select to_char(generate_series, 'yyyy-mm-dd') as time
  2.         from generate_series(to_char(now() - interval '8 DAY', 'yyyy-mm-dd') :: date,
  3.         to_char(now(), 'yyyy-mm-dd') :: date, '1 DAY')

生成近8年的日期(与上同理 )格式:yyyy

  1. select generate_series as time
  2. from generate_series(to_char(now(), 'yyyy') :: int - 8, to_char(now(), 'yyyy') :: int)

 生成近8个月的日期(格式 :yyyy-mm)

  1. SELECT TO_CHAR(NOW(), 'YYYY-MM') as time
  2. union all
  3. SELECT TO_CHAR(NOW() - INTERVAL '1 MONTH', 'YYYY-MM') as orderTime
  4. union all
  5. SELECT TO_CHAR(NOW() - INTERVAL '2 MONTH', 'YYYY-MM') as orderTime
  6. union all
  7. SELECT TO_CHAR(NOW() - INTERVAL '3 MONTH', 'YYYY-MM') as orderTime
  8. union all
  9. SELECT TO_CHAR(NOW() - INTERVAL '4 MONTH', 'YYYY-MM') as orderTime
  10. union all
  11. SELECT TO_CHAR(NOW() - INTERVAL '5 MONTH', 'YYYY-MM') as orderTime
  12. union all
  13. SELECT TO_CHAR(NOW() - INTERVAL '6 MONTH', 'YYYY-MM') as orderTime
  14. union all
  15. SELECT TO_CHAR(NOW() - INTERVAL '7 MONTH', 'YYYY-MM') as orderTime
  16. union all
  17. SELECT TO_CHAR(NOW() - INTERVAL '8 MONTH', 'YYYY-MM') as orderTime

  生成指定年月份(格式 :yyyy-mm)

  1. SELECT concat('2022-',lpad(generate_series :: text,2,'0')) as time
  2. from generate_series(1,12)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/406387
推荐阅读
相关标签
  

闽ICP备14008679号