How do I convert a date ( YYYY-MM-DD ) into a month number in postgresql? - postgresql

I got a table:
CREATE TABLE TRANSACTION (
transaction_date date,
id_transaction int,
PRIMARY KEY (id_transaction)
);
and I want to compare the month of 'transaction_date' field with a number of month.
SELECT *
FROM TRANSACTION T
WHERE month = transaction_date;
but I don't know how to make this conversion.

You can use EXTRACT(MONTH FROM transaction_date)
SELECT *
FROM transaction
WHERE EXTRACT(MONTH FROM transaction_date) = 1;
sqlfiddle demo
As per the documentation:
EXTRACT (field FROM source)
The extract function retrieves subfields such as year or hour from
date/time values. source must be a value expression of type timestamp,
time, or interval.

SELECT *
FROM TRANSACTION T
WHERE EXTRACT(MONTH FROM TIMESTAMP transaction_date) = month;
month should be an integer between 1 (January) and 12 (December).

Related

Create date column from year and doy column

Is there a way to create a date column combining one column having the year as string and one column containing a date-of-year (doy) as integer?
I am aware of methods like SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40'); or SELECT to_char(date_trunc('year', now()) + interval '169 days', 'MM/DD') but when trying to replace the "hard coded" stings with the columns I always get some kind of an error.
SELECT s.id, s.year, s.doy,
((s.year||'-01-01')::date + (s.doy||' days')::interval )::date AS date
FROM table_name AS s
the (s.year||'-01-01') or (s.doy||' days') concats the column value with a required string and the ::date or ::interval changes the resulting string type
You can use the make_date() function and add the number of days directly because date + integer is a valid operation:
select make_date(s.year, 1, 1) + s.doy as date
from ...

Extract year from big date in postgreSQL ('date out of range for timestamp')

I get an error "date out of range for timestamp" in the following situation:
select '1000000-01-01'::date; --> 1000000-01-01 -> ok!
select extract(year from ('1000000-01-01'::date)) -> date out of range for timestamp
select to_char('1000000-01-01'::date, 'YYYY') -> date out of range for timestamp
I guess the problem is that somewhere the date is converted to timestamp.
How to extract the year in my situation?
Yeah, it seems any time you do an operation on a date it gets transformed to a timestamp. The best I could come up with:
select split_part('1000000-01-01', '-', 1) as year;
year
---------
1000000
--If value is actual date:
select split_part('1000000-01-01'::date::text, '-', 1) as year;
year
---------
1000000

Updating date field with min date from selected dates if they are >= to current_date, except when all dates are >= to current date

I would like to update the contents of the Date1 column to reflect the oldest date in each row, unless the date has already passed (Date1 < current date), in which case i'd like Date1 to be populated with the 2nd oldest date in the row.
ID
Date 1
Date 2
Date 3
Date 4
001
01/14/2022
01/14/2022
01/15/2022
01/16/2022
002
04/15/2019
04/15/2019
01/10/2021
01/10/2021
I am currently using
update mytable t
set date1 = (
select min(date)
from (values (date2), (date3), (date4)) d(dt)
where dt >= current_date
)
The only problem I run into is when all available dates are prior to the current date. In this case it overwrites the value in the date1 column with null, which is not ideal. I'd like the query to leave the date1 field intact in these instances.
Figured it out:
update mytable t
set date1 = coalesce ((
select min(date)
from (values (date2), (date3), (date4)) d(dt)
where dt >= current_date
), date 1);

How to select specific dates in PostgreSQL?

My table:
create table example
(
code varchar(7),
date date,
CONSTRAINT pk_date PRIMARY KEY (code)
);
Dates:
insert into example(code, date)
values('001','2016/05/12');
insert into example(code, date)
values('002','2016/04/11');
insert into example(code, date)
values('003','2017/02/03');
My problem: how to select the previous dates to six month from today ?
In MySQL I can use PERIOD_DIFF,but, in PostgreSQL?
You can try INTERVAL instruction :
SELECT date
FROM example
WHERE date < CURRENT_DATE + INTERVAL '6 months'
AND date > CURRENT_DATE;
You will get the dates from today to six months.

Postgres where clause compare timestamp

I have a table where column is of datatype timestamp
Which contains records multiple records for a day
I want to select all rows corresponding to day
How do I do it?
Assuming you actually mean timestamp because there is no datetime in Postgres
Cast the timestamp column to a date, that will remove the time part:
select *
from the_table
where the_timestamp_column::date = date '2015-07-15';
This will return all rows from July, 15th.
Note that the above will not use an index on the_timestamp_column. If performance is critical, you need to either create an index on that expression or use a range condition:
select *
from the_table
where the_timestamp_column >= timestamp '2015-07-15 00:00:00'
and the_timestamp_column < timestamp '2015-07-16 00:00:00';