简体   繁体   中英

The difference between the two dates in PostgreSQL

Is there an equivalent to this T-SQL command in PostgreSQL?

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.

Looks like this is simply subtracting 6 months from current_timestamp, so the equivalent would be:

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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