Thread
-
Convert date and time colums to datetime
Rich Shepard <rshepard@appl-ecosys.com> — 2025-10-19T14:43:36Z
The database has a table with separate date and time columns. 1. Are there benefits to merging the two into a single timestamp column? 2. If so, how would I do this? (Reading date/time operators and functions doc page hasn't shown me one.) TIA, Rich
-
Re: Convert date and time colums to datetime
Adrian Klaver <adrian.klaver@aklaver.com> — 2025-10-19T14:53:56Z
On 10/19/25 07:43, Rich Shepard wrote: > The database has a table with separate date and time columns. > > 1. Are there benefits to merging the two into a single timestamp column? 1) One less column to fetch from. 2) If you really need a timestamp the work is already done, instead of building on the fly. > > 2. If so, how would I do this? (Reading date/time operators and functions > doc page hasn't shown me one.) select ('10/19/2025'::date + '07:50'::time)::timestamptz; timestamptz ------------------------ 2025-10-19 07:50:00-07 > > TIA, > > Rich > > > -- Adrian Klaver adrian.klaver@aklaver.com -
Re: Convert date and time colums to datetime
Adrian Klaver <adrian.klaver@aklaver.com> — 2025-10-19T14:56:57Z
On 10/19/25 07:53, Adrian Klaver wrote: > On 10/19/25 07:43, Rich Shepard wrote: >> The database has a table with separate date and time columns. >> >> 1. Are there benefits to merging the two into a single timestamp column? > > 1) One less column to fetch from. > > 2) If you really need a timestamp the work is already done, instead of > building on the fly. > >> >> 2. If so, how would I do this? (Reading date/time operators and functions >> doc page hasn't shown me one.) > > select ('10/19/2025'::date + '07:50'::time)::timestamptz; > > timestamptz > ------------------------ > 2025-10-19 07:50:00-07 Should have added in previous post, the above assumes your current date and time values are from the server timezone. > >> >> TIA, >> >> Rich >> >> >> > > -- Adrian Klaver adrian.klaver@aklaver.com -
Re: Convert date and time colums to datetime
Rich Shepard <rshepard@appl-ecosys.com> — 2025-10-19T16:35:39Z
On Sun, 19 Oct 2025, Adrian Klaver wrote: > 2) If you really need a timestamp the work is already done, instead of > building on the fly. Adrian, As each row in the table already has both a date column and a time column I don't know if I 'really' need a timestamp. When would a timestamp be really needed? > select ('10/19/2025'::date + '07:50'::time)::timestamptz; Yes, I saw that on the doc page. This requires manually changing each row in the table rather than using a date/time condition/function to to create the single timestamp column. So, apparently there's not a way to modify the table other than by hand. Thanks, Rich -
Re: Convert date and time colums to datetime
Isaac Morland <isaac.morland@gmail.com> — 2025-10-19T16:46:11Z
On Sun, 19 Oct 2025 at 12:35, Rich Shepard <rshepard@appl-ecosys.com> wrote: > On Sun, 19 Oct 2025, Adrian Klaver wrote: > > > 2) If you really need a timestamp the work is already done, instead of > > building on the fly. > > Adrian, > > As each row in the table already has both a date column and a time column I > don't know if I 'really' need a timestamp. When would a timestamp be really > needed? > > > select ('10/19/2025'::date + '07:50'::time)::timestamptz; > > Yes, I saw that on the doc page. This requires manually changing each row > in > the table rather than using a date/time condition/function to to create the > single timestamp column. So, apparently there's not a way to modify the > table other than by hand. If you're talking about actually changing the table, replacing the two columns with a single column, you would need ALTER TABLE. Something like (not tested, just to give you the basic idea): ALTER TABLE [table] ADD [new_column] timestamp; UPDATE [table] SET [new_column] = [date_column] + [time_column]; ALTER TABLE [table] DROP date_column, DROP time_column; The answer already given essentially tells you what to put in the UPDATE statement, which is an important element. The following page may help with details: https://www.postgresql.org/docs/current/sql-altertable.html Of course, all queries that touch the table need to be updated. There are some new features that might help with migration; for example, if you made a new timestamp column that is a generated column, you could have both co-existing in the table at the same time while you update the users of the table to use the new column. You also might be able to do something with defaults to allow the column adding to also populate the new column appropriately, which would allow you to just do a single ALTER TABLE and no UPDATE. I personally would almost always combine date+time into a single timestamp. It's easier for computations, and whenever you need just one all you have to do is cast to date or time as appropriate. -
Re: Convert date and time colums to datetime
Adrian Klaver <adrian.klaver@aklaver.com> — 2025-10-19T16:59:39Z
On 10/19/25 09:35, Rich Shepard wrote: > On Sun, 19 Oct 2025, Adrian Klaver wrote: > >> 2) If you really need a timestamp the work is already done, instead of >> building on the fly. > > Adrian, > > As each row in the table already has both a date column and a time column I > don't know if I 'really' need a timestamp. When would a timestamp be really > needed? I would say the fact you are asking is an indication you might need it. > >> select ('10/19/2025'::date + '07:50'::time)::timestamptz; > > Yes, I saw that on the doc page. This requires manually changing each > row in > the table rather than using a date/time condition/function to to create the > single timestamp column. So, apparently there's not a way to modify the > table other than by hand. 1) Again you seem to be establishing a need for combined value. 2) Not sure how you do "...date/time condition/function to to create the single timestamp column" without creating a column? If you meant creating a timestamp value then I point you at my original post and: "2) If you really need a timestamp the work is already done, instead of building on the fly." Doing the one time change to add a timestamp field to the table and then do the UPDATE of date + time to populate it versus calculating it every time you need the value. Of course moving forward you would need to change the code that INSERTs new values to use a timestamp instead of a date and a time value. > > Thanks, > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com -
Re: Convert date and time colums to datetime
Rich Shepard <rshepard@appl-ecosys.com> — 2025-10-19T17:56:34Z
On Sun, 19 Oct 2025, Isaac Morland wrote: > If you're talking about actually changing the table, replacing the two > columns with a single column, you would need ALTER TABLE. Something like > (not tested, just to give you the basic idea): Issac, I thought it could be that simple, while I was looking in the date/time conditions/functions. Thanks, Rich
-
Re: Convert date and time colums to datetime
Laurenz Albe <laurenz.albe@cybertec.at> — 2025-10-19T18:09:23Z
On Sun, 2025-10-19 at 07:43 -0700, Rich Shepard wrote: > The database has a table with separate date and time columns. > > 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 = CAST ((datecol + timecol) + INTERVAL '12 hours' AS date), timecol = CAST ((datecol + timecol) + INTERVAL '12 hours' AS time) WHERE id = 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 = 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 -
Re: Convert date and time colums to datetime
Rich Shepard <rshepard@appl-ecosys.com> — 2025-10-19T19:08:19Z
On Sun, 19 Oct 2025, Laurenz Albe wrote: > That depends on what you do with the table. Laurenz, That makes sense. > Are your SQL statements simple and natural with the current design? > Then stick with what you have now. That's what I'm going to do. I was curious when a timestamp column was more efficient, or otherwise preferred, since only a couple of my databases have a table with both date and time. And neither has many rows, but one could be quite large some time in the future. Thanks very much, Rich
-
Re: Convert date and time colums to datetime
Rob Sargent <robjsargent@gmail.com> — 2025-10-19T19:40:59Z
> On Oct 19, 2025, at 1:08 PM, Rich Shepard <rshepard@appl-ecosys.com> wrote: > > On Sun, 19 Oct 2025, Laurenz Albe wrote: > >> That depends on what you do with the table. > > Laurenz, > > That makes sense. > >> Are your SQL statements simple and natural with the current design? >> Then stick with what you have now. > > That's what I'm going to do. I was curious when a timestamp column was more > efficient, or otherwise preferred, since only a couple of my databases have > a table with both date and time. And neither has many rows, but one could be > quite large some time in the future. > > Thanks very much, > > Rich > I think you have to ask why those values were separated in the first place. For instance if they are thought of as a pair in most queries then an alteration might be in order. There can be a large one time cost if these tables occur in a lot of separate sql calls in the business logic. >
-
Re: Convert date and time colums to datetime
Rich Shepard <rshepard@appl-ecosys.com> — 2025-10-19T20:38:11Z
On Sun, 19 Oct 2025, Rob Sargent wrote: > I think you have to ask why those values were separated in the first > place. For instance if they are thought of as a pair in most queries then > an alteration might be in order. There can be a large one time cost if > these tables occur in a lot of separate sql calls in the business logic. Rob, Good point. They're in the contacts table and I use them to determine when to make another contact and if prior contacts were more productive in the morning or afternoon. Thanks, Rich
-
Re: Convert date and time colums to datetime
Rob Sargent <robjsargent@gmail.com> — 2025-10-20T02:32:07Z
> On Oct 19, 2025, at 2:38 PM, Rich Shepard <rshepard@appl-ecosys.com> wrote: > > On Sun, 19 Oct 2025, Rob Sargent wrote: > >> I think you have to ask why those values were separated in the first >> place. For instance if they are thought of as a pair in most queries then >> an alteration might be in order. There can be a large one time cost if >> these tables occur in a lot of separate sql calls in the business logic. > > Rob, > > Good point. They're in the contacts table and I use them to determine when > to make another contact and if prior contacts were more productive in the > morning or afternoon. > > Thanks, > > Rich > > Definitely a datetime (single value) problem, imho
-
Re: Convert date and time colums to datetime
Peter J. Holzer <hjp-pgsql@hjp.at> — 2025-10-20T11:05:38Z
On 2025-10-19 20:32:07 -0600, Rob Sargent wrote: > > On Oct 19, 2025, at 2:38 PM, Rich Shepard <rshepard@appl-ecosys.com> wrote: > > On Sun, 19 Oct 2025, Rob Sargent wrote: > >> I think you have to ask why those values were separated in the first > >> place. For instance if they are thought of as a pair in most queries then > >> an alteration might be in order. There can be a large one time cost if > >> these tables occur in a lot of separate sql calls in the business logic. > > > > Good point. They're in the contacts table and I use them to determine when > > to make another contact and if prior contacts were more productive in the > > morning or afternoon. > > Definitely a datetime (single value) problem, imho Actually, to me that seems to be one of the few cases where splitting them makes sense. I would expect typical updates to be something like "sane time, but 6 months later" or "same day, but different time". There might also be constraints like "not before 9am". For queries there might be stuff like "who do I need to call today", or as Rich already mentioned, statistics by time of the day. There are probably relatively few queries where you need to treat date and time as a unit. hjp -- _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" -
Re: Convert date and time colums to datetime
Rob Sargent <robjsargent@gmail.com> — 2025-10-20T12:43:17Z
> On Oct 20, 2025, at 5:05 AM, Peter J. Holzer <hjp-pgsql@hjp.at> wrote: > > On 2025-10-19 20:32:07 -0600, Rob Sargent wrote: >>>> On Oct 19, 2025, at 2:38 PM, Rich Shepard <rshepard@appl-ecosys.com> wrote: >>> On Sun, 19 Oct 2025, Rob Sargent wrote: >>>> I think you have to ask why those values were separated in the first >>>> place. For instance if they are thought of as a pair in most queries then >>>> an alteration might be in order. There can be a large one time cost if >>>> these tables occur in a lot of separate sql calls in the business logic. >>> >>> Good point. They're in the contacts table and I use them to determine when >>> to make another contact and if prior contacts were more productive in the >>> morning or afternoon. >> >> Definitely a datetime (single value) problem, imho > > Actually, to me that seems to be one of the few cases where splitting > them makes sense. I would expect typical updates to be something like > "sane time, but 6 months later" or "same day, but different time". There > might also be constraints like "not before 9am". For queries there might > be stuff like "who do I need to call today", or as Rich already > mentioned, statistics by time of the day. There are probably relatively > few queries where you need to treat date and time as a unit. > > hjp I don’t see any mention of the current data types of the two columns currently in play. apologies of I missed that. Which of your example updates cannot be done with timestamp? Perhaps the “not before”constraint but can that be done with OP’s design? Maybe the time column is an interval? > > -- > _ | Peter J. Holzer | Story must make more sense than reality. > |_|_) | | > | | | hjp@hjp.at | -- Charles Stross, "Creative writing > __/ | http://www.hjp.at/ | challenge!" > <signature.asc>
-
Re: Convert date and time colums to datetime
Rich Shepard <rshepard@appl-ecosys.com> — 2025-10-20T12:45:34Z
On Sun, 19 Oct 2025, Rob Sargent wrote: > Definitely a datetime (single value) problem, imho Rob, Okay. Now I'm curious: why do you write this? Thanks, Rich
-
Re: Convert date and time colums to datetime
Rich Shepard <rshepard@appl-ecosys.com> — 2025-10-20T12:51:19Z
On Mon, 20 Oct 2025, Rob Sargent wrote: > I don’t see any mention of the current data types of the two columns > currently in play. apologies of I missed that. Column | Type | Collation | Nullable | Default --------------+------------------------+-----------+----------+----------------- contact_date | date | | not null | CURRENT_DATE contact_time | time without time zone | | | Rich -
Re: Convert date and time colums to datetime
Rob Sargent <robjsargent@gmail.com> — 2025-10-20T12:53:08Z
> On Oct 20, 2025, at 6:45 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote: > > On Sun, 19 Oct 2025, Rob Sargent wrote: > >> Definitely a datetime (single value) problem, imho > > Rob, > > Okay. Now I'm curious: why do you write this? > > Thanks, > > Rich > The way I read your description of how you use these columns currently suggests to me that they could be handled by a single timestamp column. The cost/benefit of converting is another thing altogether. >
-
Re: Convert date and time colums to datetime
Rich Shepard <rshepard@appl-ecosys.com> — 2025-10-20T12:58:16Z
On Mon, 20 Oct 2025, Rob Sargent wrote: >> Okay. Now I'm curious: why do you write this? > The way I read your description of how you use these columns currently > suggests to me that they could be handled by a single timestamp column. > The cost/benefit of converting is another thing altogether. Rob, Since either way works I'll save the time cost of converting because my tables will never be really large. Thanks, Rich
-
Re: Convert date and time colums to datetime
Ron Johnson <ronljohnsonjr@gmail.com> — 2025-10-20T13:00:19Z
On Mon, Oct 20, 2025 at 8:58 AM Rich Shepard <rshepard@appl-ecosys.com> wrote: > On Mon, 20 Oct 2025, Rob Sargent wrote: > > >> Okay. Now I'm curious: why do you write this? > > > The way I read your description of how you use these columns currently > > suggests to me that they could be handled by a single timestamp column. > > The cost/benefit of converting is another thing altogether. > > Rob, > > Since either way works I'll save the time cost of converting because my > tables will never be really large. > Virtual columns exist: have both! -- Death to <Redacted>, and butter sauce. Don't boil me, I'm still alive. <Redacted> lobster!
-
Re: Convert date and time colums to datetime
Rob Sargent <robjsargent@gmail.com> — 2025-10-20T13:26:32Z
> On Oct 20, 2025, at 6:58 AM, Rich Shepard <rshepard@appl-ecosys.com> wrote: > > On Mon, 20 Oct 2025, Rob Sargent wrote: > >>> Okay. Now I'm curious: why do you write this? > >> The way I read your description of how you use these columns currently >> suggests to me that they could be handled by a single timestamp column. >> The cost/benefit of converting is another thing altogether. > > Rob, > > Since either way works I'll save the time cost of converting because my > tables will never be really large. > > Thanks, > > Rich > For sure! Your call >
-
Re: Convert date and time colums to datetime
Peter J. Holzer <hjp-pgsql@hjp.at> — 2025-10-23T19:52:29Z
On 2025-10-20 06:43:17 -0600, Rob Sargent wrote: > > > > On Oct 20, 2025, at 5:05 AM, Peter J. Holzer <hjp-pgsql@hjp.at> wrote: > > > > On 2025-10-19 20:32:07 -0600, Rob Sargent wrote: > >>>> On Oct 19, 2025, at 2:38 PM, Rich Shepard <rshepard@appl-ecosys.com> wrote: > >>> On Sun, 19 Oct 2025, Rob Sargent wrote: > >>>> I think you have to ask why those values were separated in the first > >>>> place. For instance if they are thought of as a pair in most queries then > >>>> an alteration might be in order. There can be a large one time cost if > >>>> these tables occur in a lot of separate sql calls in the business logic. > >>> > >>> Good point. They're in the contacts table and I use them to determine when > >>> to make another contact and if prior contacts were more productive in the > >>> morning or afternoon. > >> > >> Definitely a datetime (single value) problem, imho > > > > Actually, to me that seems to be one of the few cases where splitting > > them makes sense. I would expect typical updates to be something like > > "sane time, but 6 months later" or "same day, but different time". There > > might also be constraints like "not before 9am". For queries there might > > be stuff like "who do I need to call today", or as Rich already > > mentioned, statistics by time of the day. There are probably relatively > > few queries where you need to treat date and time as a unit. > > Which of your example updates cannot be done with timestamp? Perhaps > the “not before”constraint but can that be done with OP’s design? > Maybe the time column is an interval? The question isn't IMHO whether it *can* be done. Obviously a certain point in time can be represented by a timestamp or a date/time pair and both will work (as will a whole lot of different representations). The question is what feels more "natural" for the given application. Are the date and the time often used independently or are they almost always used as an atomic entity? My impression from what Rich wrote is that it might be the former. Which would suggest also storing them independently. Not saying that this is necessarily the right thing to do but isn't "definitely a datetime (single value) problem" either. hjp -- _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" -
Re: Convert date and time colums to datetime
Rob Sargent <robjsargent@gmail.com> — 2025-10-23T23:23:50Z
> On Oct 23, 2025, at 1:52 PM, Peter J. Holzer <hjp-pgsql@hjp.at> wrote: > > On 2025-10-20 06:43:17 -0600, Rob Sargent wrote: >> >> >>>> On Oct 20, 2025, at 5:05 AM, Peter J. Holzer <hjp-pgsql@hjp.at> wrote: >>> >>> On 2025-10-19 20:32:07 -0600, Rob Sargent wrote: >>>>>> On Oct 19, 2025, at 2:38 PM, Rich Shepard <rshepard@appl-ecosys.com> wrote: >>>>> On Sun, 19 Oct 2025, Rob Sargent wrote: >>>>>> I think you have to ask why those values were separated in the first >>>>>> place. For instance if they are thought of as a pair in most queries then >>>>>> an alteration might be in order. There can be a large one time cost if >>>>>> these tables occur in a lot of separate sql calls in the business logic. >>>>> >>>>> Good point. They're in the contacts table and I use them to determine when >>>>> to make another contact and if prior contacts were more productive in the >>>>> morning or afternoon. >>>> >>>> Definitely a datetime (single value) problem, imho >>> >>> Actually, to me that seems to be one of the few cases where splitting >>> them makes sense. I would expect typical updates to be something like >>> "sane time, but 6 months later" or "same day, but different time". There >>> might also be constraints like "not before 9am". For queries there might >>> be stuff like "who do I need to call today", or as Rich already >>> mentioned, statistics by time of the day. There are probably relatively >>> few queries where you need to treat date and time as a unit. >> >> Which of your example updates cannot be done with timestamp? Perhaps >> the “not before”constraint but can that be done with OP’s design? >> Maybe the time column is an interval? > > The question isn't IMHO whether it *can* be done. Obviously a certain > point in time can be represented by a timestamp or a date/time pair and > both will work (as will a whole lot of different representations). The > question is what feels more "natural" for the given application. Are > the date and the time often used independently or are they almost > always used as an atomic entity? My impression from what Rich wrote > is that it might be the former. Which would suggest also storing them > independently. Not saying that this is necessarily the right thing to > do but isn't "definitely a datetime (single value) problem" either. > > hjp > Agreed. My position is that I would have started with timestamp. OP is welcome to choose what works best for him. Certainly not seriously advocating the alter table and related effort at this point. > -- > _ | Peter J. Holzer | Story must make more sense than reality. > |_|_) | | > | | | hjp@hjp.at | -- Charles Stross, "Creative writing > __/ | http://www.hjp.at/ | challenge!" > <signature.asc>