[英]MS SQL server to postgresql
This query helps me to returns the previous month by having the actual month as parameter. 该查询通过将实际月份作为参数来帮助我返回上个月。
SELECT SUBSTRING(CONVERT(VARCHAR(10), DATEADD(MONTH, DATEDIFF(MONTH, 0, '01-'+'2014-05') - 1, 0), 105),4,10)
for example, here the query returns : '04-2014' 例如,此处查询返回:'04 -2014'
so how can i do the samething in postgresql, taking into account that postgresql doesn't use : 所以考虑到postgresql不使用,我该如何在postgresql中做同样的事情:
DATEDIFF , DATEADD functions? DATEDIFF,DATEADD函数?
Use to_char()
and to_date()
: 使用to_char()
和to_date()
:
select to_char(to_date('2014-05' || '-01', 'YYYY-MM-DD') - interval '1 month', 'MM-YYYY')
I would encourage you to use the format "YYYY-MM" instead of "MM-YYYY", if you have a choice. 如果可以的话,我建议您使用格式“ YYYY-MM”而不是“ MM-YYYY”。 Putting the year first makes it possible to order by the value. 将年份放在第一位可以按值排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.