How to store mmyy in Postgres? - postgresql

There are two variables:
$mm = "01";
$yy = "22";
I need to store it in one column Postges mmyy.
What is type mmyy must be if there is not day?
Or I can use the first day of month always like: 010122.
Eventually, I want to filter rows with where mmyy > now().

If you want to treat it like a date/time value, preferably store it as date. That would be:
date '2022-01-01'
Occupies only 4 bytes, same as int4.
Always use ISO format, which is unambiguous regardless of your locale settings. More in the manual.
To convert your variables, you might use the Postgres function to_date():
test=> SELECT to_date('2201', 'YYMM');
to_date
------------
2022-01-01
(1 row)
test=> SELECT to_date('22'::text || '01'::text, 'YYMM');
to_date
------------
2022-01-01
(1 row)
Or prepare a date literal in ISO format.
When compared to now() (which returns timestamp with time zone) the date value is coerced to the first instant of Jan 1st, 2022 at the time zone determined by the current setting of your session. See:
Difference between now() and current_timestamp
So it works as intended out of the box - except that you possibly haven't thought about time zones, yet ...
For other tasks with date arithmetic, an integer might be a good choice. See:
How do you do date math that ignores the year?

Related

How to manipulate column data in postgres

I need to manipulate column data in postgres
When I run the query-
SELECT t.date::varchar
FROM generate_series(timestamp '2020-02-27'
, timestamp '2020-03-01'
, interval '1 day') AS t(date);
it returns -
2020-02-27 00:00:00
2020-02-28 00:00:00
2020-02-29 00:00:00
2020-03-01 00:00:00
I want -
20200227
20200228
20200229
20200301
Removed '-' and truncated from end.
Can someone guide
If you don't specifically need some features of a varchar, by default use text instead.
You don't need to cast every time - generate_series() will do that automatically once it detects your step is an interval. That's unless you specifically want the generate_series(timestamp,timestamp,inteval) variant, not generate_series(timestamptz,timestamptz,inteval).
If you cast to be explicit, cast your dates as date. Regardless of whether you leave it as text literal or make them actual dates, PostgreSQL will have to cast them to match the function definition.
If you're planning to group things by a text-based date or do that to truncate timestamps, consider date_bin() and date_trunc() as well as simply holding things as a native date type. It'll take less space, run faster and enable native date-specific functions.
Make sure you're using to_char() to its full potential - it can save a lot of formatting later.
SELECT to_char(t.date,'YYYYMMDD') as date
FROM generate_series('2020-02-27'
, '2020-03-01'
, interval '1 day') AS t(date);
-- date
------------
-- 20200227
-- 20200228
-- 20200229
-- 20200301

How to extract data from DWH on a certain date? Is there the special pattern for this case in Postgres SQL?

Usually we use EXTRACT (FROM YEAR date_column) = 2000 (let it be 2000 year). Also we can add EXTRACT (MONTH FROM date_column) = 1 (let it be January). Also we can extract a day - EXTRACT (DAY FROM date_column) = 5 (let it 5). But is it possible to use the pattern for this data? How does it look like in Postgres SQL
Say we have the table Shipment, the columns - date_payment, quantity, sum.
I'd like to get the table that content all shipments for 01.01.2020
How to query this table with data format 'YYYY-MM-DD', not using EXTRACT-function?
If the date column is a date type then:
SELECT * FROM some_table WHERE date_col = '2020-01-01';
If the column is timestamp or timestamptz then:
SELECT * FROM some_table WHERE date_trunc('day', date_col) = '2020-01-01'
Beware that with timestamptz time zones come into play when doing the date_trunc. From here date_trunc:
When the input value is of type timestamp with time zone, the truncation is performed with respect to a particular time zone; for example, truncation to day produces a value that is midnight in that zone. By default, truncation is done with respect to the current TimeZone setting, but the optional time_zone argument can be provided to specify a different time zone. The time zone name can be specified in any of the ways described in Section 8.5.3.
For a timestamp value per the above link:
A time zone cannot be specified when processing timestamp without time zone or interval inputs. These are always taken at face value.

Summer and wintertime postgres timezone/timestamp

I uploaded csv data into table in postgres. One of the columns is a character varying column with an epoch filling. The data going in is as following:
Example record: 1507656308.
The epoch character varying column is transformed to a timestamp column as follows:
select
TIMESTAMP WITH TIME ZONE 'epoch' + timestamp::integer * INTERVAL '1 second' as timestamp
from
table
The result appeared good and I continued with it. I live in the Netherlands and here we have the winter-summertime change twice a year. Apparently my data does not differentiate.
I tried to figure out my current timezone. Which I did like this:
SELECT EXTRACT(TIMEZONE FROM timestamp) FROM table;
Resulting in:
3,600.0000
When I try to figure out my general time zone I get this:
show timezone
= Europe/Berlin
How do I get my timestamps to recognize the summer vs wintertime? Or what did I do wrong?
Of all the creative ways to convert an epoch value (why is it stored as string?) to a timestamp with time zone, this is one of the more interesting one.
The simple and boring way is
SET timezone = 'Europe/Berlin';
SELECT to_timestamp(1507656308);
to_timestamp
------------------------
2017-10-10 19:25:08+02
(1 row)
The time zone offset for that is two hours, because on October 10, daylight savings time was in effect (Central European Summer Time).
Indeed:
SELECT extract(timezone FROM to_timestamp(1507656308));
date_part
-----------
7200
(1 row)
So everything is working as expected, right?

Date and time in UTC - how to store them in postgres?

I am getting my data: date and time in UTC, in a csv file format in separate columns. Since I will need to convert this zone to date and time of the place where I live, currently in summer to UTC+2, and maybe some other zones I was wondering what is the best practice to insert data in postgres when we are talking about type of data. Should I place both of my data in a single column or keep them separate as types: date and time, and if not should I use timestamp or timestampz (or something else).
use timestamptz it will store your time stamp in UTC. and will display it to the client according to it's locale.
https://www.postgresql.org/docs/current/static/datatype-datetime.html
For timestamp with time zone, the internally stored value is always in
UTC (Universal Coordinated Time, traditionally known as Greenwich Mean
Time, GMT). An input value that has an explicit time zone specified is
converted to UTC using the appropriate offset for that time zone. If
no time zone is stated in the input string, then it is assumed to be
in the time zone indicated by the system's TimeZone parameter, and is
converted to UTC using the offset for the timezone zone.
When a timestamp with time zone value is output, it is always
converted from UTC to the current timezone zone, and displayed as
local time in that zone. To see the time in another time zone, either
change timezone or use the AT TIME ZONE construct (see Section 9.9.3).
updated with another good point from Lukasz, I had to mention:
Also in favor of single column is the fact that if you would store
both date and time in separate columns you would still need to combine
them and convert to timestamp if you wanted to change time zone of
date.
Not doing that would lead to date '2017-12-31' with time '23:01:01' would in other time zone in fact be not only different time, but different date with all YEAR and MONTH and DAY different
another update As per Laurenz notice, don't forget the above docs quote
An input value that has an explicit time zone specified is converted to UTC using the appropriate offset for that time zone. Which means you have to manage the input dates carefully. Eg:
t=# create table t(t timestamptz);
CREATE TABLE
t=# set timezone to 'GMT+5';
SET
t=# insert into t select '2017-01-01 00:00:00';
INSERT 0 1
t=# insert into t select '2017-01-01 00:00:00' at time zone 'UTC';
INSERT 0 1
t=# insert into t select '2017-01-01 00:00:00+02';
INSERT 0 1
t=# select * from t;
t
------------------------
2017-01-01 00:00:00-05
2017-01-01 05:00:00-05
2016-12-31 17:00:00-05
(3 rows)

Default timestamp format and fractional seconds

I'm trying to format the timestamps in my Postgres database to a certain format:
YYYY-MM-DD HH24:MI:SS
By doing:
update myTable set tds = to_char(tds, 'YYYY-MM-DD HH24:MI:SS')::timestamp;
I managed to set all the previously stored tds to this format. However, any newly added entry goes back to: YYYY-MM-DD HH24:MI:SS.MS since the default is set to now().
How do I change this so that newly added entries also have the format: YYYY-MM-DD HH24:MI:SS?
There is no format stored in a timestamp type. You can set its default to a timestamp truncated to the second at creation time
create table t (
tds timestamp default date_trunc('second', now())
)
Or alter the table
alter table t
alter column tds
set default date_trunc('second', now());
insert into t values (default);
INSERT 0 1
select * from t;
tds
---------------------
2014-03-11 19:24:11
If you just don't want to show the milliseconds part format the output
select to_char(now(), 'YYYY-MM-DD HH24:MI:SS');
to_char
---------------------
2014-03-11 19:39:40
The types timestamp or timestamptz optionally take a precision modifier p: timestamp(p).
To round to full seconds, set the default to:
now()::timestamp(0)
or:
now()::timestamptz(0)
Standard SQL functions CURRENT_TIMESTAMP (returns timestamptz) or LOCALTIMESTAMP (returns timestamp) allow the same precision modifier:
CURRENT_TIMESTAMP(0)
LOCALTIMESTAMP(0)
That's a bit shorter than calling date_trunc() - which truncates fractional seconds (may be what you really want!)
date_trunc('second', now())
Store timestamps as timestamptz (or timestamp), not as character type.
Finally, to make sure that ...
newly added entries also have the format: YYYY-MM-DD HH24:MI:SS
you could define your column as type timestamptz(0). This covers all values entered into that column, not just the default. But the rounding may introduce timestamps up to half a second in the future. If that can be an issue in any way, rather use date_trunc().
See #Clodoaldo's answer for instructions on to_char() and how to ALTER TABLE.
This related answer for in-depth information on timestamps and time zone handling:
Ignoring time zones altogether in Rails and PostgreSQL