alter column (using + where) - sql

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

Related

Is there an alternative to CURRENT_TIMESTAMP with no timezone?

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)

alter column from time with time zone to timestamp

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

Change column type from character varying to timestamp without time zone

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.

Update Oracle table records timezone values

I have a table with an Oracle column of TIMESTAMP (6) WITH TIME ZONE. The table contains records of varying timezones. I'd like to update all of the records of the table to be UTC. Is there a recommended way of doing this in an UPDATE query? I've looked at the Oracle methods to_date(), which is for converting a string to a date, and from_tz() which converts a time to a time with timezone.
It seems like I'd need a way to run a query and pull the timezone from the field, and then somehow update the field to put it in UTC. I don't want to simply change the timezone designation, I want to offset the time of day so
21-JAN-10 03.28.38.713000000 PM -05:00
would become
21-JAN-10 08.28.38.713000000 PM UTC
.
CREATE TABLE "MyDb"."Books"
(
"GUID" RAW(32) DEFAULT SYS_GUID(),
"DATE_CREATED" TIMESTAMP (6) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
);
You should be able to do something like
UPDATE "Books"
SET date_created = date_created at time zone 'UTC'
which will do something like this
SQL> create table foo( col1 timestamp with time zone );
Table created.
SQL> insert into foo values( current_timestamp );
1 row created.
SQL> select * from foo;
COL1
---------------------------------------------------------------------------
13-FEB-12 01.38.42.372000 PM -05:00
SQL> update foo
2 set col1 = col1 at time zone 'UTC';
1 row updated.
SQL> select * from foo;
COL1
---------------------------------------------------------------------------
13-FEB-12 06.38.42.372000 PM UTC
Now, as a style matter, creating case-sensitive table names and column names is highly discouraged as it will greatly annoy the developers that have to maintain the code.

mysql automatically store record creation timestamp

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;