Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vAXqW-002syN-VH for pgsql-general@arkaria.postgresql.org; Sun, 19 Oct 2025 18:09:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1vAXqV-00Bkfi-K5 for pgsql-general@arkaria.postgresql.org; Sun, 19 Oct 2025 18:09:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1vAXqV-00BkfZ-8Z for pgsql-general@lists.postgresql.org; Sun, 19 Oct 2025 18:09:30 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vAXqR-0035SI-22 for pgsql-general@postgresql.org; Sun, 19 Oct 2025 18:09:29 +0000 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-471076f819bso28856275e9.3 for ; Sun, 19 Oct 2025 11:09:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1760897365; x=1761502165; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=jTWEXowrsn7uAyHTOHHVwFCfkZSrtLILdyUJ/KSSClI=; b=DKhhOQJuE6UMuzuDTQKJovvTzdieBk1MDWiWvUN1oFAECJOkjF4S3LWf43xziJUFsA kw4d/9bWiIm5QrQHG45TIi+0VXEa82gnhqn7VBMhLeci4aegQlU+7beYm2fOVRgJ5Hah SU35eh+b1MwnwQnmZPwtTZWZ6eotikvqBXUltWVRwVHo2P/UMGid1SUjO7823cLyHSmE 1cDaPaFHL6GRy7T5l2/LWz2I2oPKGJfrNDX2LZPecNA+PJPPHixpxshC0L2EmOedq3G6 f6ejGa+fP+RR/kgoYl6SbSn/Uwm1sN6HGJfOherS5rR1sX9tB2HMrlGU4/iCWgtTaFxj RJWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760897365; x=1761502165; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=jTWEXowrsn7uAyHTOHHVwFCfkZSrtLILdyUJ/KSSClI=; b=oOTURvRMEmEHc1GlHxzoDzM1eSvO1Pi7ClAZFDHN1/HnrvOy5TkCE09cqwmqi06Nnm bB3QLZVAb8fuvZmPZBjB8/uk2WMqOuaebAgTAGQocgczW4AMctVzzn+qJ1KVWD00bqht j+u+UMa8as+llGYnYgDF04eS9jbz7sApq5v9jsSlzLk1vIK0smGwDf22uLq2Eir4G9E+ G7mVd7gEh52hkIgT9Bz4Fr4qZUPNEVsFAHoKW/pS85mzUuAforq3rsbAQTno38d8XX08 aJNDSEzntcg/vJzYpNoe9LQ1Mr9grp13YUFUNUrIUsV38rulc/cj1AfFW9vLDBq31ahI pXXg== X-Forwarded-Encrypted: i=1; AJvYcCUCFAovkNye5UMiEjuxAMx1Dj/IkItqKGOWvvphADmwGBPFPKv2vr6Y4Pm6jfPsEDi6PySCQg2lsnZZn0vg@postgresql.org X-Gm-Message-State: AOJu0YwBNqEUjjaVRkXYnKIpYJ2za4RhSc125w5n0DXx5saScdXAziUV Xec022H89jBvboghFCCDcMttW1cjjLTmPrL5XNtfmBEMxZCLRF64awcqLkzTc2+XhJrRafPfpUP N5IH8 X-Gm-Gg: ASbGnct3TGPBuM9ULhLs2eiV37J40NbGBXCseAKzmysRad815fiBlR/jlxQgixmg1Y1 kZwB7dC/B1kc2LuHgZaQcsj6Ywq/yiD9ENGg+P2ga+F56aj0Acwmhv7i6M4IdpqDt34FZ3r2yME 6UeIEV3ZajUzPpoUNJRVWM/zGSGPfcYwj1KkxinFGG7Ux15g4zBzVuLAkyLp6QoY6aGVUzxwXXI P+PeCDaGF+Yl55NkE9kEA9gdcU9/41YTB7wTzS/mNg6hOoGGT5ku2cE7MlopZQaiCCNhCMNAMYB ORMppEd9yGFDdkQBpoPE9Gr4Obqmi+oZB2SjW1cr4zAw61qbxVHt60LporXEaYK5UcgYihg98mA t+MAKnf1A6Ux83PINoCry25iDm3IS45YKmAudlHz1c9bfXGR/HW0jKHJWEF/XS4GrQXmk+y3tJ3 bVYOeLO4OGL6ozITrPYNonqaLTrpZ9 X-Google-Smtp-Source: AGHT+IFZB0qsPiQrvl9gDwOYiUDar3NcN9zAaHrsx7PuSi56BQ2Touhm/O7CrEoueCExgzYsPyiTNg== X-Received: by 2002:a05:600c:46cc:b0:45d:84ca:8a7 with SMTP id 5b1f17b1804b1-4711789b16cmr74461375e9.14.1760897364865; Sun, 19 Oct 2025 11:09:24 -0700 (PDT) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:70:1a83:5c31:e322:326:8abf]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-471144238easm195568105e9.4.2025.10.19.11.09.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Oct 2025 11:09:24 -0700 (PDT) Message-ID: <6c2fadf7fb9f5b0f1728b0baf9f112f3204415fe.camel@cybertec.at> Subject: Re: Convert date and time colums to datetime From: Laurenz Albe To: Rich Shepard , pgsql-general@postgresql.org Date: Sun, 19 Oct 2025 20:09:23 +0200 In-Reply-To: <63dfbc7c-bc63-7fa-a51b-915dd804ea2@appl-ecosys.com> References: <63dfbc7c-bc63-7fa-a51b-915dd804ea2@appl-ecosys.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-2.fc42) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, 2025-10-19 at 07:43 -0700, Rich Shepard wrote: > The database has a table with separate date and time columns. >=20 > 1. Are there benefits to merging the two into a single timestamp column? That depends on what you do with the table. Are your SQL statements simple and natural with the current design? Then stick with what you have now. Do you need date arithmetic that is awkward with the current design? Then a timestamp column is better. If you design the table from scratch, a timestamp column is almost always the right thing. But if the current design works good enough, you may save yourself the pain of restructuring the table. For convenience with queries, you could use a view or virtual generated column, as shown below. An example for an awkward statement that would strongly indicate that a timestamp is a better choice: UPDATE tab SET datecol =3D CAST ((datecol + timecol) + INTERVAL '12 hours' AS date), timecol =3D CAST ((datecol + timecol) + INTERVAL '12 hours' AS time) WHERE id =3D 42; > 2. If so, how would I do this? (Reading date/time operators and functions > doc page hasn't shown me one.) - If you want to modify the table: ALTER tab ADD timestampcol timestamp; /* will take a long time if the table is big */ UPDATE tab SET timestampcol =3D datecol + timecol; /* will take a long time if the table is big */ ALTER TABLE tab ALTER timestampcol SET NOT NULL; ALTER TABLE tab DROP datecol, DROP timecol; /* to get rid of the bloat from the UPDATE */ VACUUM (FULL) tab; - If you just want to use the timestamp in queries, use a view: CREATE VIEW v AS SELECT *, datecol + timecol AS timestampcol FROM tab; - As an alternative to the previous, you can create a virtual generated column in the base table from v18 on: ALTER TABLE tab ADD timestampcol timestamp GENERATED ALWAYS AS (datecol + timecol= ); Yours, Laurenz Albe