我想在postgresql中创建包含两年的自定义类型,我该怎么做?

时间:2022-10-03 19:25:35

I would like to create custom type which contain two years in postgresql, how i could do this? only years without months and days

我想在postgresql中创建包含两年的自定义类型,我该怎么做?只有几年没有几个月和几天

2 个解决方案

#1


0  

SQL DEMO

CREATE TYPE twoYears AS
   (year1 date,
    year2 date);

DROP TABLE IF EXISTS myTable;  

CREATE TABLE myTable (
   ty twoYears
);    

INSERT INTO myTable VALUES (ROW('2010-01-01', '2020-01-01'));

SELECT (ty).year1,  (ty).year2
FROM myTable;

SELECT EXTRACT(YEAR FROM (ty).year1),
       EXTRACT(YEAR FROM (ty).year2)
FROM myTable;

#2


0  

 create table mytable (rng daterange)

daterange is already a type in postgresql. https://www.postgresql.org/docs/9.3/static/rangetypes.html

daterange已经是postgresql中的一个类型。 https://www.postgresql.org/docs/9.3/static/rangetypes.html

#1


0  

SQL DEMO

CREATE TYPE twoYears AS
   (year1 date,
    year2 date);

DROP TABLE IF EXISTS myTable;  

CREATE TABLE myTable (
   ty twoYears
);    

INSERT INTO myTable VALUES (ROW('2010-01-01', '2020-01-01'));

SELECT (ty).year1,  (ty).year2
FROM myTable;

SELECT EXTRACT(YEAR FROM (ty).year1),
       EXTRACT(YEAR FROM (ty).year2)
FROM myTable;

#2


0  

 create table mytable (rng daterange)

daterange is already a type in postgresql. https://www.postgresql.org/docs/9.3/static/rangetypes.html

daterange已经是postgresql中的一个类型。 https://www.postgresql.org/docs/9.3/static/rangetypes.html