简体   繁体   English

PostgreSQL中两个日期之间的差异

[英]The difference between the two dates in PostgreSQL

Is there an equivalent to this T-SQL command in PostgreSQL? PostgreSQL中有与此T-SQL命令等效的命令吗?

SELECT COUNT(*) 
FROM [dbo].[LayerTable] 
where layerType=3 
 and created >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp))),0)

I have found date_part() and extract function() but not working. 我发现date_part()和提取function(),但不能正常工作。

Looks like this is simply subtracting 6 months from current_timestamp, so the equivalent would be: 看起来这只是从current_timestamp中减去6个月,因此等效为:

SELECT COUNT(*) 
FROM dbo.layer_table
where layer_type=3 
  and created >= current_timestamp - interval '6 months';

If you want the start of the month (rather than the "same" day as "today") as the result use: 如果希望将月初(而不是“今天”与“今天”)作为结果,请使用:

created >= date_trunc('month', current_timestamp - interval '6 months')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM