Postgres SQL Date conversion - postgresql

I have a field stored as timestamp without time zone which looks like this 2017-12-26 06:56:54.958. When I convert it like this:
to_char(date_field, 'DD/MM/YYYY hh24:mm:ss')
I end up with 26/12/2017 06:12:54. Does anybody know why? And if so, is there a way around this. As you can see, 44 minutes are removed from the date giving a time of 06:12 rather than 06:56.
I have tried to convert to UTC and GMT but I have had no luck. Any help would be greatly appreciated.

Try
to_char(date_field, 'DD/MM/YYYY HH24:MI:SS')
MI is the pattern for minutes. MM is for months, and the case is irrelevant.

Related

CAST datetime to date misalignment in RedShift

I need to covert a DATETIME field to DATE in order to join two datasets. No biggy, CAST(DATETIME) as DATE. However, the following is happening:
Datetime
Date
2022-12-02T22:00:00
2022-12-03
2022-12-02T08:00:00
2022-12-02
Clearly something is going on with the interpretation of the times. The DATETIME is on GMT while my system is on EST. My guess is that CAST is reading the datetime as the system's time zone and converting it to GMT on more time. Does anyone have any thoughts on what is happening and how to work around this?
I have tried CAST, CONVERT, TRUNC, AT TIME ZONE... All of them still gives the same result. The result I am searching for is the following:
Datetime
Date
2022-12-02T22:00:00
2022-12-02
2022-12-02T08:00:00
2022-12-02
Thanks!
This is due to your default timezone.
You can change for the current session like so:
SET timezone TO 'UTC';
or equivalently:
SET timezone TO 'GMT';
To change session back to default timezone:
set timezone to default;
https://docs.aws.amazon.com/redshift/latest/dg/r_timezone_config.html

Transform Timestamp Without Time Zone to a simple date in PostgreSQL

Suppose I have a column that has the datatype Timestamp without Time Zone and looks like this:
Sun Jan 01 1933 00:00:00 GMT+0000 (Coordinated Universal Time)
I want to change it to a simple date like this 01/01/1933.
I have tried the following, but there is no change in the output:
SELECT timestamp_date::date as new_date,
DATE(timestamp_date) as also_date
FROM my_schema.my_table;
What am I doing wrong here?
you can use the function to_char
to_char(timestamp_date, 'DD/MM/YYYY');
Or you can play with DATESTYLE configuration parameters, but use to_char always as possible, to avoid the change of configuration

how to add a plus sign and milliseconds to a date in postgresql?

the date is like 2021-07-23
the expected output is like this 2021-07-24 00:00:00+08 notice the +08 ?
I tried like this below, but i am not getting the plus symbol
s_table.school_date + INTERVAL '1 day'
any idea how to make that +08 appear ?
I haven't been able to test it, but have you tried casting that date to a timestampz?
See this page from postgresql tutorial on the different timestamp types.
So you could try:
(s_table.school_date + INTERVAL '1 day')::timestampz
to cast your current value to a timestamp with a timezone.
If the timezone returned is incorrect you can use SET timezone='America/Los_Angeles' or find the timezone your current db is using with SHOW timezone; You can also see all timezones for your database with SELECT * FROM pg_timezone_names;.
I got the timezone specific information from this resource.
I would be sure to test out setting the timezone in a transaction first as I'm concerned it would set the timezone for your entire database and that might affect other procedures / queries happening in your database.
I hope this helps! Sorry I couldn't try it out first before sending this information along.
edit: looks like I was a bit slow! Happy you figured it out :)
That's the time with time zone included.
Try running the following in Postgres and it will return you the datetime with time zone which is after + or - depending on where you are.
SELECT NOW();
Edit-01:
try the following code:
s_table.school_date::timestamptz + INTERVAL '1 day'

Convert CURRENT_DATE or UNIX_TIMESTAMP to string in Hive (YYYY-DD-MM format)

I have a weird dataset that I need to access in hive. Using traditional date/time functions (such as dateadd, etc) have proven difficult/ineffective.
There is a column in my dataset that is a string with the date in YYYY-MM-DD format.
I am wondering if it's possible to get the current date in YYYY-MM-DD format, and somehow cast it as a string?
I've done considerable amounts of research and have tried just about everything in the documentation (https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions)
Any help here will be greatly appreciated, as I've been grinding away at this problem for a considerable amount of time :)
Thanks!
I didn't get how the date will be displayed for format YYYY-MM-DD, but you can try below.
hive> select cast(from_unixtime(unix_timestamp(cast(current_date() as string), 'yyyy-MM-dd'),'YYYY-MM-DD') as string);
OK
2018-09-271
Time taken: 0.114 seconds, Fetched: 1 row(s)
hive>
from_unixtime accepts string format and you can get all the options here - https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
hope this helps.
VIJAY

Converting mysql date from one format to another

I have date stored in a field like this: 31st Dec 2013 but I need to alter it to something like this: 2013-12-31 00:00:00
I have tried lots of variations of DATE_FORMAT and strtotime but got nowhere. How do I go about this please?
Thanks
The DATE_FORMAT() function is used to display date/time data in different formats.
Try this:
DATE_FORMAT(your_date,'%Y %m %d %T:%f')
Use a date_format inside another date_format. The inner one detects the format you are giving, the outer one outputs what you need.