I am having trouble changing a column called end_date in a table called key_request from time with time zone to timestamp in my Postgres database . I have tried using the following code:
alter table key_request alter column end_date type timestamp with time zone using end_date::timestamp with time zone
I keep getting the following error:
ERROR: cannot cast type time with time zone to timestamp with time zone
Any idea of how I can adjust this query to work?
you can do something like this:
alter table key_request
alter column end_date type timestamp with time zone using date('20130101') + end_date;
sql fiddle demo
I woul do this in a series of steps
Alter the table, adding a new column end_date1 as time with time zone
Copy the date from end_date(old) to end_date1
Alter the table, droping the old end_date column
Alter the table,reaming end_date1 to end_date
Changing from java.sql.Date to java.util.Date
ALTER TABLE key_request ALTER COLUMN end_date TYPE timestamp without time zone;
If you have dependant views that relate to that table
drop views
change column type
recreate views
Solution coming from a java/hibernate approach while in type migration.
java.sql.Date is translated to date
java.util.Date is translated to timestamp without time zone
Related
I need to store the date a row was inserted into a table, and so far I have this:
CREATE TABLE mytable (
inserted_on timestampz DEFAULT CURRENT_TIMESTAMP
);
This works, but really I don't need the time zone (which is included by timestampz). Is there an alternative to CURRENT_TIMESTAMP that doesn't return the time zone?
I think you are looking for localtimestamp. The data type is timestamp without timezone:
select localtimestamp, pg_typeof(localtimestamp)
I work with postgresql 9.3,
I use this query to convert timestamp column to UTC.
ALTER TABLE table
ALTER timestamp TYPE timestamp
USING timestamp AT TIME ZONE 'UTC' ;
I need to add WHERE cluose to the query :
ALTER TABLE table
ALTER timestamp TYPE timestamp where timestamp < '2015-01-06 00:00:00'
USING timestamp AT TIME ZONE 'UTC' ;
but, this is didn't work .
there no clear resource on how to do it.
You could apply ALTER TYPE construction only to whole column, not to some rows. If you want to convert timestamp value you should use UPDATE instead.
you can't alter table column type using where, because "where" is DML and "alter" is DDL. Use update whe
In Rails I created a string column called open_time, but then I realized I should use the datetime type. I did:
change_column :polls, :open_time, :datetime
But it said:
PG::Error: ERROR: column "open_time" cannot be cast to type timestamp without time zone
: ALTER TABLE "polls" ALTER COLUMN "open_time" TYPE timestamp
If I just drop the string column and add new datetime column, I will lose the data stored in the string column. Alternatively, in PostgreSQL I can add a column:
ALTER TABLE polls ADD COLUMN published_time timestamp;
Ant then try to get the data from the string column like:
UPDATE polls SET published_time = strToTimeStamp(open_time);
Are there any functions I can use as strToTimeStamp to convert character varying type to timestamp without time zone?
.. are there any functions I can use as strToTimeStamp that can convert
character varying type to timestamp without time zone type?
Use to_timestamp() to convert string data to type timestamp and alter the data type of the column in place:
ALTER TABLE tbl ALTER COLUMN col TYPE timestamp
USING to_timestamp(col, '<your pattern here>');
See:
Alter character field to date
Cast varchar type to date
Don't think you can do that, since it's limited by capability of database. The idea is doing following:
Created a new column with a different name
Copy over the column contents after data convert for each row
Drop the original col
Rename the new col to to the original col name
These steps can be placed into same migration file and keep them into a transaction for in case.
Is there some way mysql can store timestamp automatically in a record row whenever that it is created. I was trying to use timestamp(data type) with current_timestamp as default value but then realised this will get updated everytime the record is updated. I just need something that will store create timestamp.
Thanks
Set the DEFAULT constraint to use CURRENT_TIMESTAMP:
CREATE TABLE ...
your_date_column DATETIME DEFAULT CURRENT_TIMESTAMP
...
For an existing table, use the ALTER TABLE statement:
ALTER TABLE your_table
ALTER COLUMN date_column SET DEFAULT CURRENT_TIMESTAMP
Unless you specify a value to for the date_column, the default will be the date & time the INSERT statement was run. NULL and DEFAULT or valid values to use the default constraint otherwise, assuming the column is nullable.
You can get the full details on timestamps in MySQL at https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html.
The point that you care about is that if you define a timestamp column as DEFAULT CURRENT_TIMESTAMP clause and don't have an ON UPDATE clause, the column has the current timestamp for its default value but is not automatically updated.
But be warned. The obvious thing to want to do is to have two timestamp columns, one being the creation time and the other being the last update time. Unfortunately it is a documented MySQL limitation that MySQL does not support this. I have no idea why MySQL has such an odd limitation - no other major database has problems with this common use case.
FYI = "Datetime" is date and time fixed. "Timestamp" is variable date and time-- system time.
So, Have two columns. One Create Col, One Update Col.
The following command will create a hello table
1. id integer
2. create_at with current time.
create table hello (id int, created_at datetime DEFAULT CURRENT_TIMESTAMP);
Create Table myTableName
(
userId int primary key
userJoiningDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
https://www.mysqltutorial.org/mysql-timestamp.aspx
Here is how you can create a column in which the time stamp is recorded when it is created. If you want to know How to update timeStamp each time that row is changed/updated, Check the above link.
SELECT * FROM test WHERE timestamp >= CURDATE() AND timestamp < CURDATE() + INTERVAL 1 DAY ORDER BY timestamp;
Bakground:
I've got a legacy app I'm working on that uses DATE types for most time storage in the database. I'd like to try update some of these tables so that they can utilize time zones since this is causing problems with users in different areas from where the db is(see A below). This is for Oracle 10g.
Quetions:
1) Can I migrate this "in place." That is can I convert like so
DATE_COL = type:DATE => DATE_COL = type:TIMESTAMP
...or will I have to use a different column name?
Keep in mind that data needs to be retained. If this can be done semi-easily in a migration script it will work for my purposes.
2) Will this type of conversion be backwards compatible? We likely have some scripts or reports that will hit this table that we may not know about. We can probably deal with it but I'd like to know what sort of hornet's nest I'm walking into.
3) What pitfalls should I be on the lookout for?
Thanks,
EDIT:
(partly in response to Gary)
I'm fine with a multi-step process.
1) move data to a new Timestamp column (caled TEMP) with some sort of conversion
2) drop old column (we'll call it MY_DATE)
3) create new timestamp column with the old date column name (MY_DATE)
4) move data to the MY_DATE column
5) drop TEMP column
A Gary also wanted clarification on the specific timezone issue. I copied my answer from below to keep it more readable.
Basically the data will be accessed from several different areas. We need to be able to convert to/from the local time zone as needed. We also have triggers that use sysdate further complicating things. timestamp with time zone alleviates a lot of this pain.
Oh and thanks for the answers so far.
You could just run:
ALTER TABLE your_table MODIFY your_date_column TIMESTAMP WITH TIME ZONE;
But I would recommend adding a TIMESTAMP column to the table, using an UPDATE statement to populate, and drop the original date column if you so choose:
ALTER TABLE your_table ADD date_as_timestamp TIMESTAMP WITH TIME ZONE;
UPDATE your_table
SET date_as_timestamp = CAST(date_column AS TIMESTAMP WITH TIME ZONE);
The conversion is backwards compatible - you can switch back & forth as you like.
Simple enough to demonstrate
SQL> create table x (y date);
Table created.
SQL> insert into x select sysdate from dual;
1 row created.
SQL> commit;
Commit complete.
SQL> alter table x modify y timestamp;
Table altered.
SQL> select * from x;
Y
---------------------------------------------------------------------------
03/NOV/09 12:49:03.000000 PM
SQL> alter table x modify y date;
Table altered.
SQL> select * from x;
Y
---------
03/NOV/09
SQL> alter table x modify y timestamp with time zone;
alter table x modify y timestamp with time zone
ERROR at line 1:
ORA-01439: column to be modified must be empty to change datatype
SQL> alter table x modify y timestamp with local time zone;
Table altered.
SQL> alter table x modify y date;
Table altered.
So you can go from date to timestamp (or timestamp with local timezone) and back again, but not for timestamp with time zone (ie where the offset is persisted).
You'd have to add another column, and copy the existing data over (with a default for the appropriate time zone).
"causing problems with users in different areas from where the db is".
Might help to be a bit more specific. Is it sufficient to convert the dates (or timestamps) from the database timezone to the user's timezone when inserted/changed/queried, or do you actually need to persist the fact that the record was created at 3:00pm in a specific timezone.