Format of date / time values in database tables - postgresql

I am reading a csv file with date fields of formatted mm/dd/yyyy. I expected the same kind of format from a Postgres table after the import, but I see yyyy-mm-dd hh:mm:ss.
The date fields in my table are defined as timestamp without time zone data type.
How do I maintain the same format of data? I am using PostgreSQL 9.3.

Postgresql only stores the value, it doesn't store formatting (which would waste space).
You can use the to_char function in your query if you like to get the output formatted in a special way. Details are in the manual.

Related

Unable to transform string to datetime/timestamp in specific format in ADF dataflow

Trying to convert string value(2022-07-24T07:04:27.5765591Z) into datetime/timestamp to insert into SQL table in datetime format without losing any value till milliseconds. String which I am providing is actually a datetime and my source is ADLS CSV. I tried below options in data flow.
Using Projection-> Changed the datatype format for specific column into timestamp and format type-yyyy-MM-dd'T'HH:mm:ss.SSS'Z' however getting NULL in output.
Derived column-> Tried below expressions but getting NULL value in output
toTimestamp(DataLakeModified_DateTime,'%Y-%m-%dT%H:%M:%s%z')
toTimestamp(DataLakeModified_DateTime,'yyyy-MM-ddTHH:mm:ss:fffffffK')
toTimestamp(DataLakeModified_DateTime,'yyyy-MM-dd HH:mm:ss.SSS')
I want the same value in output-
2022-07-24T07:04:27.5765591Z (coming as string) to 2022-07-24T07:04:27.5765591Z (in datetime format which will be accepted by SQL database)
I have tried to repro the issue and it is also giving me the same error, i.e., null values for yyyy-MM-dd'T'HH:mm:ss.SSS'Z' timestamp format. The issue is with the string format you are providing in source. The ADF isn’t taking the given string as timestamp and hence giving NULL in return.
But if you tried with some different format, like keeping only 3 digits before Z in last format, it will convert it into timestamp and will not return NULL.
This is what I have tried. I have kept one timestamp as per your given data and other with some modification. Refer below image.
This will return NULL for the first time and datetime for second time.
But the format you are looking for is still missing. With the existing source format, the yyyy-MM-dd'T'HH:mm:ss would work fine. This format also works fine in SQL tables. I have tried and it’s working fine.
Try to use to String instead of timestamp and use this to create your Desired timestamp
toString(DataLakeModified_DateTime, 'yyyy-MM-dd HH:mm:ss:SS')

How do I create a specific date in PostgreSQL?

I need to execute a INSERT statement writing a date with a YYYY-MM-DD format.
Would to_date('2021-09-28','YYYY-MM-DD') work?
YYYY-MM-DD is the the ISO 8601 standard date format and unambiguous default in Postgres. Just insert your date literally.
The type date is stored as a 4-byte integer quantity internally, which does not preserve any format. You can format any way you like on output with some basic locale settings or settings of your client, or explicitly with to_char().
Input with to_date('2021-09-28','YYYY-MM-DD') works, too. But you don't need to_date() while operating with ISO format.

SQLite Convert to SQLite Datetime Format from ANY datetime format

Coming out of an Oracle background converting dates from any format to any format is really easy.
How is this done in SQLite? I've searched and searched for answers and most of the answers simply say... Save your date/strings in SQLite in one single format which is YYYY-MM-DD HH:MM:SS.SSS. This seems rigid to me.
I don't have that luxury as my data is stored in this format DD/MM/YYYY HH:MI:SS am ex. 3/7/2020 8:02:31 AM.
NOTE: For single days/months my date values do not contain leading zeros and my time is NOT in military time.
How do I tell SQLite what my date format is so that I can correctly convert my stored dates to SQLite datetime formats?
Convert from SQLite Date Format to Oracle Date Example:
In Oracle I would simply use the to_date function like so
to_date('2019-03-07 15:39:34', 'YYYY-MM-DD HH24:MI:SS')
All one needs to do is to tell the function what the date format is... and then it spits out a date.... easy peasy. What this example does is convert a SQLite date formated string to a date that Oracle recognizes as a date. It doesn't matter what the format is in as I tell the function what format to expect.
How do I Convert Dates in SQLite from any format to the SQLite Format?
Converting from SQLite's date format string to ANY date is easy as there are functions built in that do this easily... but how to do this the other way round?

How to insert time into redshift

Redshift only support TIMESTAMP format, I want to dump some data that is originally stored in Postgres in time without time zone format. My data looks like this: 15:30:00
When I COPY my data into Redshift, it says Invalid timestamp format or value [YYYY-MM-DD HH24:MI:SS], error code 1206.
My current workaround is to create that column in Redshift as CHAR(8)
Thanks
There's no other solution that to use the char(8).
Redshift does not support timezones.
Workaround would be to parse data to UTC and store time difference in separate column. You will be able to calculate local time by adding/substracting hours to the UTC data.

Postgresql Ethiopian Date Format

Is there a way to store a date in a PostgreSQL db using the Ethiopian date format? I'm trying to store 29th or 30th of February but it throws an error, because in the Julian calendar there's no such thing. Any inputs?
I am not sure that I'll tell you something new but...
Databases are used by programs or by interfaces, I never saw databases that are used by end-user in console with psql.
If you are develop an application, that must display dates in specific calendar, you can store date in PostgreSQL in TIMESTAMP. All operations with dates will work correct in database. But you have to implement conversion from TIMESTAMP into string representation and vice versa in your application manually. If this is most important thing for your application, you will do this.
All queries that must return date you will write with conversion into DOUBLE PRECISION e.g.
SELECT EXTRACT(EPOCH FROM timestamp_field)
This returns DOUBLE PRECISION value that represents timestamp in numerical format.
All date parameters in queries you have convert from numerical presentation in TIMESTAMP using built-in function to_timestamp:
update table_name set
timestamp_fileld = to_timestamp(1384852375.46666)
The other solution is to write psql functions that do this for you directly in queries, but anyway you need to handle each input/output of date fields in queries.