date_format function in Hive gives wrong output - sql

I am trying to format date and time down to millis as in the following code:
date_format(date_and_time, 'YYYY-MM-dd HH:mm:ss.SSS') as date_and_time
However if I am querying the date_and_time value prior to formatting and after formatting I get
two different results:
2021-02-03 04:09:14.367 vs 2021-12-31 20:54:20.504
Normally I should get same 2021-02-03 04:09:14.367, am I doing something wrong?
This example does have initially milliseconds values but I have some data in the dataset that don't.
Thank you

solved by formatting with 'yyyy-MM-dd HH:mm:ss.SSS'

Related

correct to_char date syntax to have trailing zeroes after milliseconds

My current query in oracle sql for getting a timestamp format is TO_CHAR(c2.start_on,'DD-MM-YY HH:MI:SS.FF PM'), it outputs the timestamp like this 25-11-20 07:00:13.36 PM
However I want it to display the date in this way 25-11-20 07:00:13.360000000 PM
What should I add in the timestamp format for this to be possible ?
I have tried doing it like this HH:MI:SS.FM00000 as suggested here
but it gives me the error. ORA-01821: date format not recognized
what is the correct way to get the date in the desired format ?
If you want fractional seconds, you don't want a DATE, you want a TIMESTAMP. So here's a timestamp formatted with 6 digits of precision
select to_char(systimestamp, 'HH:MI:SS.FF6') from dual;
If you have a date, you could convert it to a TIMESTAMP (using CAST AS TIMESTAMP), but better to look at updating your data model to use the proper type for the source column as starters.

Not able to add time in SQL

I have been learning SQL,and I was trying out this command:
SELECT TO_CHAR (SYSDATE,'HH:MM:SS'),TO_CHAR(SYSDATE+INTERVAL '15' MINUTE,'HH:MM:SS') FROM DUAL;
However the output I am getting is
I even tried out this command:
SELECT TO_CHAR (SYSDATE,'HH:MM:SS'),TO_CHAR(SYSDATE+15/1440,'HH:MM:SS') FROM DUAL;
Even then I am getting this output:
Can anyone explain why am I not getting the second time as 15 minutes ahead of the current system date?
PS: I am using Oracle LiveSQL.
As mentioned by Barbaros Özhan in comments
The MM format model is for months. You want the MI format model for minutes.
Additionally, the HHformat model is an abbreviation for HH12 and will show the hours in a 12-hour clock. You want HH24 to use a 24-hour clock.
SELECT TO_CHAR(SYSDATE,'HH24:MI:SS') AS now,
TO_CHAR(SYSDATE+INTERVAL '15' MINUTE,'HH24:MI:SS') AS now_plus_15
FROM DUAL;
Outputs:
NOW
NOW_PLUS_15
12:38:13
12:53:13
db<>fiddle here

convert date to midnight in hive

I want to truncate a specific date to midnight time in hive.
I have tried the following below, but it puts 12 hour time instead of 00.
date_format(MIN(date_and_time), 'yyyy-MM-dd hh:00:00.000')
Result obtained
2021-11-03 12:00:00.000
Any suggestions on that?
Thank you
Use HH for a 24-hour clock:
date_format(MIN(date_and_time), 'yyyy-MM-dd HH:00:00.000')

Extract hour from timestamp in 12 hour format with AM/PM indicator oracle

I'm trying to extract hour from time-stamp column (oracle) in 12 hour format with A.M/P.M indicator.
The column "subdate" has value 22-APR-20 04.18.40.000000000 PM
The Extract(HOUR from subdate) function returns
16
However, I want the value to be 4.00 PM
I have tried various options with the trunc and to_char functions but none of them gave the desired output.
Can you please help with this!
Thank you!
Since you are talking about formats, you are not simply "extracting the hour" - you are really extracting a string.
TO_CHAR(subdate, 'hh.mi AM')
should do the trick.
ADDED: Based on comments. If you want : separator instead of . that is easy, just change it in the format mask. If you want to round down to the hour use trunc(subdate, 'hh') instead of subdate. If you need to drop the leading zero, you can wrap the entire expression in LTRIM(... , '0') or, as Alex Poole suggest, use the fm toggle like this: 'fmhh:fmmi AM'

Epoch_to_date in oracle

It seems as though the epoch_to_date function is adding some hours to the date I pass. Can anyone point out what am I missing here ? I am converting a date to epoch and converting back to date expecting to get the same value I passed. But I am getting a different value.
When I run the following query in my Db,
select epoch_to_date(to_epoch(to_date('07/31/2014 10:35:46','mm/dd/yyyy HH24:MI:SS'))) from dual;
The output is
EPOCH_TO_DATE(TO_EP
-------------------
31/07/2014 17:35:46
Not understanding why 7 hrs got added to my date. Please help.
Epoch_to_Date gives you the date in GMT format. Hence it is 7 hrs ahead, please convert accordingly by using new_time function.
Please refer below.
to_char(new_time(epoch_to_date(CREATE_DATE),'GMT','PDT'),'dd-mm-yyyy HH24:MI:SS')
CREATE_DATE