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')
Related
What I need to do is pretty simple.
I just need to update a DATE field in SQL to a PM time.
Only thing is, if I use the TO_DATE function to update to an AM time, no problem...
TO_DATE('2021-09-30 11:00:00', 'YYYY-MM-DD HH:MI:SS')
However if I try to do the same thing to set to a PM time using military time...
TO_DATE('2021-09-30 23:00:00', 'YYYY-MM-DD HH:MI:SS')
It says that the hour has to be between 1 and 12.
Any suggestions?
Thanks
According to the Oracle documentation, in order to convert a time in 24 hour format, you need to use HH24. When you use just HH, Oracle assumes the time to be in AM/PM format, i.e. that the number must be between 1 and 12.
So you need to change your code to the following
TO_DATE('2021-09-30 23:00:00', 'YYYY-MM-DD HH24:MI:SS')
Refer to this db<>fiddle
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'
So i have some timestamps in a DB and i want to get the hours and minutes difference from them
The problem is the timestamp portion is formatted incorrectly where the hour is always 12 and the minutes portion is actually the hours and the seconds is actually the minutes.
Example DB timestamp: 10/1/2020 12:08:52 AM
So in the above example the time is actually 8:52 AM not 12:08 AM
How can i convert this datetime to something i can use in order to calculate the difference in minutes and hours between these 2 oddly formatted timestamps?
My ideal end goal is something that displays the difference in the HH:MM format
EDIT: the timestamps in oracle actually look like below, and in this eaxmple the 12 means nothing and 18 is actually the hours.
Example of what I'm looking for:
01-OCT-20 12.18.44.000000000 AM - 01-OCT-20 12.12.42.000000000 AM
Output: 06:02 . so the timespan would be 6 hours and 2 minutes in this case.
Thanks,
You can turn your string to an Oracle date (resp timestamp) with to_date() (resp to_timestamp()):
to_timestamp(mystring, 'dd/mm/yyyy ss:hh12:mi am')
Then you can use date arithmetics to compute the difference. Substrating timestamps gives you an interval, which is pretty much what you seem to be looking for, so:
to_timestamp(mystring1, 'dd/mm/yyyy ss:hh12:mi am')
- to_timestamp(mystring2, 'dd/mm/yyyy ss:hh12:mi am')
as myinterval
Like so?
(my default date format is 'yyyy-mm-dd hh24:mi:ss' in Oracle ...)
WITH
indata(sdb) AS (
SELECT '10/1/2020 12:08:52 AM' FROM dual
UNION ALL SELECT '10/1/2020 12:08:52 PM' FROM dual
)
SELECT
TO_TIMESTAMP(sdb,'dd/mm/yyyy 12:hh:mi AM') AS ts
FROM indata;
-- out ts
-- out ---------------------
-- out 2020-01-10 08:52:00
-- out 2020-01-10 20:52:00
I am using Redshift and am looking to extract the time from the timestamp.
Here is the timestamp: 2017-10-31 23:30:00
and I would just like to get the time as 23:30:00
Is that possible?
In Redshift you can simply cast the value to a time:
the_timestamp_column::time
alternatively you can use the standard cast() operator:
cast(the_timestamp_column as time)
Please go through this link
http://docs.aws.amazon.com/redshift/latest/dg/r_Dateparts_for_datetime_functions.html
timezone, timezone_hour, timezone_minute
Supported by the DATE_TRUNC function and the EXTRACT for time stamp
with time zone (TIMESTAMPTZ)
Examples is here
select extract(minute from timestamp '2009-09-09 12:08:43');
select extract(hours from timestamp '2009-09-09 12:08:43');
Time is visible on my table as follows
2017-09-15 16:30:00.000
And my Query is like this
Format(SlotStartTime,'dd/MM/yyyy hh:mi:ss') as SlotStartTime
And this returns time like this
15/09/2017 04:30:00
So the calculation gets wrong it should be 15/09/2017 04:30:00 PM or stay as it is in 24-hour format. How can I achieve this?
hh gives you 12 hour hours
HH gives you 24 hour hours
Also, mi is probably a typo, as it will return you a minute, followed by the letter i. Try this:
declare #datetime datetime = '2017-08-29 16:30:01'
select Format(#datetime,'dd/MM/yyyy HH:mm:ss') as SlotStartTime
You can find all valid datetime formatting characters here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
hh is the code for 12 hour format. HH is the code for 24 hour format. It's similar to how mm (not mi) is for minutes and MM is for months.
You want:
FORMAT(SlotStartTime,'dd/MM/yyyy HH:mm:ss')
12 hour with the time period:
FORMAT(SlotStartTime,'dd/MM/yyyy hh:mm:ss tt')