Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert empty date into postgres

Tags:

postgresql

I have a column that requires a timestamp with time zone but I cannot use a built in pgSQL function nor can I modify the column to accept null dates. How can I insert a string date in the right format manually that represents no time?

like image 967
Dan Avatar asked Jun 15 '18 23:06

Dan


People also ask

How do I format date in PostgreSQL?

The TO_DATE function in PostgreSQL is used to converting strings into dates. Its syntax is TO_DATE(text, text) and the return type is date. The TO_TIMESTAMP function converts string data into timestamps with timezone. Its syntax is to_timestamp(text, text) .

Is empty string null in PostgreSQL?

Handling empty strings in PostgreSQL In Oracle, because empty strings are treated as NULL, the preceding insert statements #2 and #3 will store NULL for column tname in the table. However, in PostgreSQL, the table will store NULL for the #2 and an empty string for the #3 insert statements.

How do you handle null values in a date column?

The DATE and TIMESTAMP functions do exactly what you are looking for. If you have a STRING column where its format is like TIMESTAMP , you can simply apply it. Then, DATE will extract just the date and it takes care of the NULL values.

How do I get the default date in PostgreSQL?

The PostgreSQL CURRENT_DATE function returns the current date (the system date on the machine running PostgreSQL) as a value in the 'YYYY-MM-DD' format. In this format, 'YYYY' is a 4-digit year, 'MM' is a 2-digit month, and 'DD' is a 2-digit day. The returned value is a date data type.


1 Answers

There is no empty timestamp other than null, but you can use one of the special date/time values infinity or -infinity.

select 'infinity'::timestamp, '-infinity'::timestamp

 timestamp | timestamp 
-----------+-----------
 infinity  | -infinity
(1 row)

See Table 8.13. Special Date/Time Inputs.

like image 92
klin Avatar answered Oct 15 '22 08:10

klin