1、数据库版本:PostgreSQL 14.5
2、psql时间函数:
-- 日期:下个月当天 2023-03-10
SELECT to_date((to_char(( now() + interval '1 month'),'YYYY-MM-DD')),'YYYY-MM-DD')
-- 日期:下个月第一天 2023-03-01
SELECT to_date((to_char(( now() + interval '1 month'),'YYYY-MM-01')),'YYYY-MM-DD')
-- 日期:上个月第一天 2023-01-01
SELECT to_date((to_char(( now() + interval '-1 month'),'YYYY-MM-01')),'YYYY-MM-DD')
-- 日期:本月底 2023-02-28
SELECT (to_date((to_char(( now() + interval '1 month'),'YYYY-MM-01')),'YYYY-MM-DD')-1)::date
SELECT CAST (now() AS timestamp)
SELECT now(),current_timestamp,current_date,current_time,localtime,loca1ltimestamp;
select now() + interval '1 min';
select now() + interval '1 days';
select now() + interval '1 month';
select now() + interval '1 years';
select to_char(now(),'yyyymmddHHMMSS')
3、psql存过实例:
CREATE OR REPLACE PROCEDURE "public"."histable"()
AS BODYBEGIN
do $$
declare
i INTEGER := 10000;
vehicleId VARCHAR(36);
checkintime TIMESTAMP;
checkouttime TIMESTAMP;
declarationNo VARCHAR(50);
licensePlateNo VARCHAR(100);
licensePlateNoBack VARCHAR(100);
taskid VARCHAR(32);
measurevehicleId VARCHAR(32);
seqid VARCHAR(32);
uuId VARCHAR(36);
BEGIN
WHILE i <= 109999 LOOP
i = i + 1;
SELECT INTO vehicleId to_char(now()+ interval '-1 month','yyyymmdd')||CEIL(random()*(1000000000000000-1)+1);
SELECT INTO measurevehicleId 'M'||ceil(random()*(1000000000000000-1)+1);
SELECT INTO checkintime CAST(now()+ interval '-1 month' AS timestamp);
SELECT INTO checkouttime CAST(now()+ interval '-1 month' + '1 min' AS timestamp);
SELECT INTO declarationNo 'DN'||floor(random()*(1000000000-1)+1);
SELECT INTO licensePlateNo 'la' || ceil(random()*(10000000000-1)+1);
SELECT INTO licensePlateNoBack 'lb' || ceil(random()*(10000000000-1)+1);
SELECT INTO taskid 'Alakoll'||ceil(random()*(100000000000000-1)+1);
SELECT INTO uuId 'UUID'||ceil(random()*(100000000000000-1)+1);
-- SELECT INTO uuId gen_random_uuid();
-- vehicleId := CEIL(random()*(1000000000000000-1)+1);
-- measurevehicleId := 'M'||ceil(random()*(1000000000000000-1)+1);
-- checkintime := CAST(now() AS timestamp);
-- checkouttime := CAST(now() + '1 min' AS timestamp);
-- declarationNo := floor(random()*(1000000000-1)+1);
-- licensePlateNo := 'la' || ceil(random()*(10000000000-1)+1);
-- licensePlateNoBack := 'lb' || ceil(random()*(10000000000-1)+1);
-- taskid := 'Alakoll'||ceil(random()*(10000000000-1)+1);
-- seqid := to_char(now(),'yyyymmddHHMMSS');
-- uuId := gen_random_uuid();
INSERT INTO "public"."VEHICLE_INSPECTED_HIS" ("ID", "ALARM_LOGISTICS", "ALARM_TIMEOUT", "ANIMAL_CONCLUSION_TYPE", "ANIMAL_OPERATION_USER_NAME", "ARC_COUNT", "ASYCUDA_CODE", "BLACK_VEHICLE", "BUSINESS_TYPE", "CHECK_IN_ARRIVAL_TIME", "CHECK_IN_TIME", "CHECK_OUT_ARRIVAL_TIME", "CHECK_OUT_TIME", "COMBINE_STATUS", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CONTAINER_NUMBER", "CREATION_TIME", "DC_CONCLUSION_TIME", "DECLARATION_NUMBER", "DEST_PORT_CODE", "DEST_PORT_NUMBER", "DIRECTION", "ENTRY_TUNNEL", "HEALTH_CONCLUSION_TYPE", "HEALTH_OPERATION_USER_NAME", "INSPECTED_PORT_CODE", "INSPECTED_PORT_NUMBER", "JOINT_INSPECTION_TIME", "LICENSE_PLATE_NUMBER", "LICENSE_PLATE_NUMBER_BACK", "LOCKED", "LOCKED_TIME", "LOCKED_USER_ACCOUNT", "LOCKED_USER_ID", "LOCKED_USER_NAME", "LOGISTICS", "LOGISTICS_FINISH", "LOGISTICS_REQUEST", "NORMAL_END", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "ORIGIN_PORT_CODE", "ORIGIN_PORT_NUMBER", "ORIGIN_VEHICLE_ID", "PI_CONCLUSION_TIME", "PLANT_CONCLUSION_TYPE", "PLANT_OPERATION_USER_NAME", "LANE_NUMBER", "SITE", "RELEASE", "RELEASE_CONFIRM_TIME", "RFID_NUMBER", "RISK_LEVEL", "SUSPICION_MARK", "SYN_STATUS", "TRAILER", "TRANSFERED_CENTRE", "TRANSIPORT_CONCLUSION_TYPE", "VEHICLE_HEIGHT", "VEHICLE_LENGTH", "VEHICLE_STATUS", "VEHICLE_TYPE", "VEHICLE_WEIGHT", "VEHICLE_WIDTH", "XRAY_PRIP_SUCCESS", "XRAY_SAVE_PATH", "XRAY_SCAN", "XRAY_SCAN_END_TIME", "XRAY_SCAN_ID", "XRAY_SCAN_START_TIME", "DCIN_MODE", "PRD_ALARM", "SENDED_CENTRE", "ADDED_PIN", "XRAY_SCAN_STATUS", "DCIN_CONCLUSION_TYPE", "PIN_CONCLUSION_TYPE", "ARC_FACTOR_TYPE", "SEIZED_POSITION", "SEIZED_GOODS", "ANIMAL_LOCK_USER_ACCOUNT", "HEALTH_LOCK_USER_ACCOUNT", "MEASURE", "MEASURE_FINISH", "PLANT_LOCK_USER_ACCOUNT", "SELECT_LANE", "TASK_ID", "WHITE_LIST", "XRAY_SCAN_FINISH", "PLAN_LANENUMBER", "DC_EXPORT_COUNT", "SCAN_MODE", "PLAN_LANENUMBER_SCAN_MODE", "PRINT_COUNT", "SCAN_RESULT") VALUES (vehicleId, 'f', 'f', 'PASSED', 'wgp', NULL, NULL, 'f', 'EXPORT', checkintime, checkintime, NULL, checkouttime, 'f', 'No Suspect', 'RELEASE', 'SIIU8005669,SIIU6033094', checkintime, checkouttime, declarationNo, NULL, NULL, 'EXPORT', 'f', 'PASSED', 'wgp', 'Alakol', '10002', NULL, licensePlateNo, licensePlateNoBack, 'f', NULL, NULL, NULL, NULL, 'f', 'f', 'f', 't', checkouttime, 'wgp', '7a4ae50c-c255-4206-95f2-9aaef19e816f', 'wgp', 'Alakol', '10002', NULL, NULL, 'PASSED', 'wgp', '1', 'ICIA', 't', checkouttime, NULL, NULL, 'f', 'f', 'f', 'f', 'PASSED', NULL, NULL, 'FINISHCHECKED', 'TRUCK', NULL, NULL, 't', '/nii/97201MB02/2021/0608/97201MB0220210608003/', 't', checkintime, '97201MB0220210608003', checkintime, 'LOCAL', 'f', NULL, NULL, 'f', 'PASS', NULL, NULL, NULL, NULL, NULL, NULL, 't', 't', NULL, 'SCAN', taskid, NULL, 't', '1', 0, 'FS', 'J', 0, 'SUCCESS');
INSERT INTO "public"."VEHICLE_INSPECTION_HIS" ("ID", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CREATION_TIME", "END_TIME", "INSPECTION_STEP", "LICENSE_PLATE_NUMBER", "MODIFY_TIME", "MODIFY_USER_ACCOUNT", "MODIFY_USER_ID", "MODIFY_USER_NAME", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "PASS", "START_TIME", "VEHICLE_ID") VALUES (uuId||'a', '通过', 'PASSED', checkintime, checkouttime, 'ANIMALCHECK', NULL, NULL, NULL, NULL, NULL, checkintime, 'wgp3', 'd30b62af-879e-4996-af1a-0cd74c632c5d', 'wgp3', 't', checkintime, vehicleId);
INSERT INTO "public"."VEHICLE_INSPECTION_HIS" ("ID", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CREATION_TIME", "END_TIME", "INSPECTION_STEP", "LICENSE_PLATE_NUMBER", "MODIFY_TIME", "MODIFY_USER_ACCOUNT", "MODIFY_USER_ID", "MODIFY_USER_NAME", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "PASS", "START_TIME", "VEHICLE_ID") VALUES (uuId||'b', '通过', 'PASSED', checkintime, checkouttime, 'PLANTCHECK', NULL, NULL, NULL, NULL, NULL, checkintime, 'wgp3', 'd30b62af-879e-4996-af1a-0cd74c632c5d', 'wgp3', 't', checkintime, vehicleId);
INSERT INTO "public"."VEHICLE_INSPECTION_HIS" ("ID", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CREATION_TIME", "END_TIME", "INSPECTION_STEP", "LICENSE_PLATE_NUMBER", "MODIFY_TIME", "MODIFY_USER_ACCOUNT", "MODIFY_USER_ID", "MODIFY_USER_NAME", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "PASS", "START_TIME", "VEHICLE_ID") VALUES (uuId||'c', '通过', 'PASSED', checkintime, checkouttime, 'HEALTHCHECK', NULL, NULL, NULL, NULL, NULL, checkintime, 'wgp3', 'd30b62af-879e-4996-af1a-0cd74c632c5d', 'wgp3', 't', checkintime, vehicleId);
INSERT INTO "public"."VEHICLE_INSPECTION_HIS" ("ID", "CONCLUSION_REMARK", "CONCLUSION_TYPE", "CREATION_TIME", "END_TIME", "INSPECTION_STEP", "LICENSE_PLATE_NUMBER", "MODIFY_TIME", "MODIFY_USER_ACCOUNT", "MODIFY_USER_ID", "MODIFY_USER_NAME", "OPERATION_TIME", "OPERATION_USER_ACCOUNT", "OPERATION_USER_ID", "OPERATION_USER_NAME", "PASS", "START_TIME", "VEHICLE_ID") VALUES (uuId||'d', '遣返', 'REPATRIATE', checkintime, checkouttime, 'TRAFFICCONFIRM', licensePlateNo, NULL, NULL, NULL, NULL, checkintime, 'wgp3', 'd30b62af-879e-4996-af1a-0cd74c632c5d', 'wgp3', 't', checkintime, vehicleId);
END LOOP;
END $$;
ENDBODY
LANGUAGE plpgsql