A
A
Alexander2021-01-19 00:23:27
PostgreSQL
Alexander, 2021-01-19 00:23:27

How to write the current time in a column, with a specific format?

The following table is present:

CREATE TABLE platforms (
  date_added time NOT NULL
) ;


How can I write to a date_added column with data type time the current time in the following format? :
YYYY-MM-DD HH24:MI:SS (should look like 2021-01-18 23:22:15 )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-01-19
@Alexandre888

In PosegreSQL, this is solved like this:

CREATE TABLE platforms (
  id serial,
  name varchar(64),
  date_added TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO platforms (name) VALUES ('Test platform');

SELECT * FROM platforms;

check SQL query
Data is stored in a TIMESTAMP type field, if you need a different format - do it when fetching For example:
SELECT id, name, to_char(date_added, 'dd/mm/YYYY') FROM platforms;

K
ky0, 2021-01-19
@ky0

Only timethe time is stored, no date. Use a timestamp .
There is a now() function that you can use if you want the DBMS to substitute the current time itself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question