如何将MMYY存储在Postgres中?
有两个变量:
$mm = "01";
$yy = "22";
我需要将其存储在一个列Postges mmyy
中。
如果没有一天,则必须是什么类型 mmyy
是什么?
或者我可以使用一个月的第一天,总是喜欢: 010122
。
最终,我想用过滤行,其中mmyy>现在()
。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想将其视为日期/时间值,最好将其存储为
日期
。那将是:仅占4个字节,与
int4
。始终使用ISO格式,无论您的语言环境设置如何,它都是明确的。 在手册中更多。
postgres函数:
或准备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: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()
:Or prepare a date literal in ISO format.
When compared to
now()
(which returnstimestamp with time zone
) thedate
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: