在PostgreSQL中,可以使用变量来存储日期时间值,并对这些变量进行各种操作。一些常见的日期时间操作包括:
SELECT CURRENT_TIMESTAMP;
DO $$
DECLARE
my_date TIMESTAMP := '2022-01-01 12:00:00';
BEGIN
-- do something with my_date
END $$;
DO $$
DECLARE
my_date TIMESTAMP := '2022-01-01 12:00:00';
BEGIN
my_date := my_date + INTERVAL '1 day';
my_date := my_date - INTERVAL '1 hour';
END $$;
SELECT TIMESTAMP '2022-01-01 12:00:00' - TIMESTAMP '2021-12-01 12:00:00' AS date_diff;
SELECT EXTRACT(YEAR FROM TIMESTAMP '2022-01-01 12:00:00') AS year,
EXTRACT(MONTH FROM TIMESTAMP '2022-01-01 12:00:00') AS month,
EXTRACT(DAY FROM TIMESTAMP '2022-01-01 12:00:00') AS day;
这些是一些基本的日期时间操作,在实际应用中可以根据需求进行更复杂的操作和计算。