如何将MMYY存储在Postgres中?

发布于 2025-02-10 12:25:42 字数 238 浏览 1 评论 0 原文

有两个变量:

$mm = "01";
$yy = "22";

我需要将其存储在一个列Postges mmyy 中。 如果没有一天,则必须是什么类型 mmyy 是什么? 或者我可以使用一个月的第一天,总是喜欢: 010122

最终,我想用过滤行,其中mmyy>现在()

There are two variables:

$mm = "01";
$yy = "22";

I need to store it in one column Postges mmyy.
What is type mmyy must be if there is not day?
Or I can use the first day of month always like: 010122.

Eventually, I want to filter rows with where mmyy > now().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2025-02-17 12:25:43

如果您想将其视为日期/时间值,最好将其存储为 日期 。那将是:

date '2022-01-01'

仅占4个字节,与 int4
始终使用ISO格式,无论您的语言环境设置如何,它都是明确的。 在手册中更多。

postgres函数

test=> SELECT to_date('2201', 'YYMM');
  to_date   
------------
 2022-01-01
(1 row)

test=> SELECT to_date('22'::text || '01'::text, 'YYMM');
  to_date   
------------
 2022-01-01
(1 row)

或准备ISO格式的文字日期。

NOW()进行比较(返回带有时区>的时间戳) date 值被胁迫到2022年1月1日的第一瞬时时区由会话的当前设置确定。请参阅:

区别't考虑了时区,但是……


对于具有日期算术的其他任务,整数可能是一个不错的选择。请参阅:

If you want to treat it like a date/time value, preferably store it as date. That would be:

date '2022-01-01'

Occupies only 4 bytes, same as int4.
Always use ISO format, which is unambiguous regardless of your locale settings. More in the manual.

To convert your variables, you might use the Postgres function to_date():

test=> SELECT to_date('2201', 'YYMM');
  to_date   
------------
 2022-01-01
(1 row)

test=> SELECT to_date('22'::text || '01'::text, 'YYMM');
  to_date   
------------
 2022-01-01
(1 row)

Or prepare a date literal in ISO format.

When compared to now() (which returns timestamp with time zone) the date value is coerced to the first instant of Jan 1st, 2022 at the time zone determined by the current setting of your session. See:

So it works as intended out of the box - except that you possibly haven't thought about time zones, yet ...


For other tasks with date arithmetic, an integer might be a good choice. See:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文