Need to return only month(yyyy-mm) instead (yyyy-mm-dd hh-mm-ss) from date_trunc() postgres sql function

Viewed 115

I'm new to sql and currently learning on postgresql, so I'm trying a command that returns aggregate value by month

SELECT date_trunc('month',"BDATE") MTH,
       COUNT("CUSTOMER_REQUEST_ID") DELIVERIES 
FROM orders 
GROUP BY MTH

but it returns (yyyy-mm-dd hh-mm-ss) but I need only (yyyy-mm) is there any function/code that would return that particular field?

1 Answers

That is because the type is still a timestamp and what you see is the default formatting. You can use to_char to do your own formatting.

SELECT to_char("BDATE", 'YYYY-MM') MTH,
       COUNT("CUSTOMER_REQUEST_ID") DELIVERIES 
FROM orders 
GROUP BY MTH
Related