Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在psql中增加一个紧凑的monthdate_Date_Postgresql_Sql Date Functions - Fatal编程技术网

在psql中增加一个紧凑的monthdate

在psql中增加一个紧凑的monthdate,date,postgresql,sql-date-functions,Date,Postgresql,Sql Date Functions,我有一个紧凑的日期字符串,例如代表2010年1月的'201001' 由于遗留原因,我无法更改日期的存储方式,但希望使用east方法将日期增加一个月。如果是日期类型,我可以使用date(compactdate+interval'1 month'),但将其转换为date类型将返回2020 10 01..导入时间 从日期时间导入日期时间 从dateutil.relativedelta导入relativedelta 打印“以(yymm)格式输入日期” 响应日期=原始输入() 新建日期=datetime.

我有一个紧凑的日期字符串,例如代表2010年1月的'201001'

由于遗留原因,我无法更改日期的存储方式,但希望使用east方法将日期增加一个月。如果是日期类型,我可以使用date(compactdate+interval'1 month'),但将其转换为date类型将返回2020 10 01..

导入时间 从日期时间导入日期时间 从dateutil.relativedelta导入relativedelta

打印“以(yymm)格式输入日期”

响应日期=原始输入()

新建日期=datetime.strTime(响应日期,%Y%m')+relativedelta(月份=+1)

打印“新日期”,新日期

输出:-

turkesh@turkesh-桌面:~/desktop/py文件$python date2.py

以(年/月/日)格式输入日期

201001

新日期2010-02-01 00:00:00


turkesh@turkesh-桌面:~/desktop/py files$

最后,我发现的唯一解决方案是凌乱的,但基本上只是使用了模,这可能是因为日期不应该以这种格式出现在系统中,尤其不是作为字符串

select (('201112'::int / 100) + ((('201112'::int % 100) +1) / 12) )*100  + ((('201112'::int % 100) +1) % 12);

这在脚本中是一件非常简单的事情,但我正在寻找一个Sql解决方案,以便它可以在视图中使用。最后,我的解决方案是断言字符串日期是一个数字,并根据结果和模的余数计算新日期