从日期postgres中提取星期数 [英] extract week number from date postgres

查看:128
本文介绍了从日期postgres中提取星期数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将周数提取为:

2015-52

,格式为:

2015-12-27

如何在postgres中执行此操作?

How can I perform this in postgres?

我的星期是从星期一到星期日计算的。

my weeks are calculated from monday to sunday.

推荐答案

字符值,请使用 to_char()

To get the year and the week in a single character value, use to_char()

select to_char(current_date, 'IYYY-IW');

IW 返回年份和星期数为在ISO标准中定义的 IYYY 返回相应的年份(可能是上一年)。

IW returns the year and the week number as defined in the ISO standard and IYYY returns the corresponding year (which might be the previous year).

如果需要以年和周为编号,请使用 提取

If you need the year and the week number as numbers, use extract

select extract('isoyear' from current_date) as year, 
       extract('week' from current_date) as week;

这篇关于从日期postgres中提取星期数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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