create automatic datetime(date with timestamp) when update the data in postgresql(phppgadmin) on database table
Plese advise me
thanks
1.if you use timestamp type in the column when you update the record then date time also updated
2.if you use varchar type in the column when you fire a query
demo
$sql = "update table_name set field_name=now() where your condition";
Related
I have a below requirement.
I want to insert records into a table using a stored procedure with below parameters
CREATE TABLE Mytable (MyPassword VARCHAR(10),PasswordDateTime DateTime)
My stored procedure is as follows to insert data into the above table.
CREATE PROCEDURE [dbo].[spPassword_Insert]
-- Parameters
#Password VARCHAR(200)
,#PasswordDateTime VARCHAR(20)
AS
SELECT #PasswordDateTime = CAST(#PasswordDateTime AS DATETIME)
INSERT INTO Mytable
SELECT #Password,#PasswordDateTime
I get the value of #PasswordDatetime from stored procedure as '2020-01-13 12:19:43.02'
I am getting the #PasswordDateTime value as string from the stored procedure and I want to convert the value data type as Date-time as per table definition without changing the value format.I want to insert the value as it is but the data type is to be changed.
While I am trying to convert a #PasswordDateTime value into date-time format, I am getting Conversion failed when converting date and/or time from character string error.
Please suggest how to convert this.
I got the answer.
DECLARE #PasswordDateTime VARCHAR(50)='2015-12-02 20:40:37.8130000'
SELECT #PasswordDateTime =cast(#PasswordDateTime as datetime2(2))
SELECT #PasswordDateTime
Thanks
I want to use a UTC timestamp as default value for a TIMESTAMP column. We're using DB2 9.5 on Linux
I'm aware of using CURRENT TIMESTAMP, but it provides the local time of the DB2 server (e.g. CEST). In queries you can use
SELECT JOB_ID, (CURRENT TIMESTAMP - CURRENT TIMEZONE) as tsp FROM SYSTEM_JOBS;
but this does not work in the column definition
ALTER TABLE SYSTEM_JOBS ALTER COLUMN CREATED SET DEFAULT CURRENT TIMESTAMP - CURRENT TIMEZONE
[42601][-104] An unexpected token "ALTER TABLE SYSTEM_JOBS ALTER COLUMN CREAT" was found following "BEGIN-OF-STATEMENT". Expected tokens may include: "<values>".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.23.42
I also tried to define a function, that makes the calculation.
CREATE OR REPLACE FUNCTION UTCTIMESTAMP ()
RETURNS TIMESTAMP
LANGUAGE SQL
DETERMINISTIC
NO EXTERNAL ACTION
BEGIN ATOMIC
DECLARE L TIMESTAMP;
DECLARE U TIMESTAMP;
SET L = CURRENT TIMESTAMP;
SET U = L - CURRENT TIMEZONE;
RETURN U;
END
;
But it's also not accepted in column definition
ALTER TABLE SYSTEM_JOBS ALTER COLUMN CREATED SET DEFAULT UTCTIMESTAMP();
[42894][-574] DEFAULT value or IDENTITY attribute value is not valid for column "CREATED" in table "DB2INST1.SYSTEM_JOBS". Reason code: "7".. SQLCODE=-574, SQLSTATE=42894, DRIVER=4.23.42
I'm looking for a method to set the default value in neutral UTC.
You are not able to use expressions in the DEFAULT clause. See the description of default-clause of the CREATE TABLE statement.
You can use a BEFORE INSERT trigger instead, for example, to achieve the same functionality:
CREATE TABLE SYSTEM_JOBS (ID INT NOT NULL, CREATED TIMESTAMP NOT NULL) IN USERSPACE1;
CREATE TRIGGER SYSTEM_JOBS_BIR
BEFORE INSERT ON SYSTEM_JOBS
REFERENCING NEW AS N
FOR EACH ROW
WHEN (N.CREATED IS NULL)
SET CREATED=CURRENT TIMESTAMP - CURRENT TIMEZONE;
INSERT INTO SYSTEM_JOBS(ID) VALUES 1;
We are making use of tRedshiftOutputBulk exec and we have set it to 'Drop table if exists and create' as an action on table. The problem is that a Date field of with a pattern of 'yyyy-MM-dd HH:mm:ssZ' is being created as Timestamp rather than TimestampTZ on Redshift.
#mark
What pattern have you given in Date format and Time format field of the component?
you can use the change time pattern 'yyyy-MM-dd HH:mm:ssTZ'
I have a table with DateCreated and DateUpdated columns and using Entity Framework to insert/update values to the database.
I need the DateCreated column to get the SQL Server's GetDate() value on insertion only.
DateUpdated column value should always get updated with current GetDate() value on insertion and update both.
For the DateCreated column, I've set StoreGeneratedPattern="Computed" and on SQL Server table I've set default value of the column to be GetDate(), which works nicely as expected.
For the DateUpdated column I could not find a way to get the GetDate() value automatically set each time an entry is updated. This value get's set only when an entry is inserted.
Could someone shed some light on this.
If you want DateUpdated to be set by the database, you can use a trigger to set the value to getdate(). I believe EF will also get the value set by the trigger if you set StoreGeneratedPattern="Computed" for DateUpdated.
For reference, your trigger would look something like this (you'll have to update per your table's PK):
create trigger MyTable_UpdatedTrigger
on MyTable
for update
as
begin
update t set DateUpdated = getdate()
from MyTable t
join inserted i on t.Id = i.Id
end
I have a sybase 15 DB and for one of my tables, I want to make a column default to the current date/time of the row insert. Is this possible?
In a sybase text, the following is said:
ALTER TABLE sales_order
MODIFY order_date DEFAULT CURRENT DATE
On my DB this doesn't do anything, as CURRENT DATE is not recognized.
using getDate() is a valid solution, you must have had a syntax error. Try it like this:
create table test_tbl (
date_data DATETIME default getDate() NOT NULL
)
Try using getDate() instead
... DEFAULT GETDATE() is correct. the case is irrelevant; mixed case may indicate a Java method, but it is a straight TSQL Function. Please post the exact error msg if you want further assistance.
Also, the ALTER TABLE method sets the Default for future INSERTS; if you want the existing data changed, you need to UPDATE (good for small tables) or unload/reload the table (demanded for the large).
Watch the NULL/NOT NULL: you do not want to change that without understanding. Again, the existing/future issue needs address. NOT NULL prevents NULL being explicitly passed as an INSERT VALUE.
CURRENT_DATE is a SQL standard that isn't universally adopted.
As noted elsewhere the getdate() T-SQL function should be used instead.