I want to subtract 1 second from a given timestamp in hive. When I am trying to add 1 second to a given timestamp, then it is working good, but subtracting is not working good.
The below statement is giving me correct result for addition
select from_unixtime(unix_timestamp('2016-11-06 01:00:00.000','yyyy-MM-dd HH:mm:ss.SSS'),'yyyy-MM-dd HH:mm:ss.SSS') , from_unixtime(unix_timestamp('2016-11-06 01:00:00.000','yyyy-MM-dd HH:mm:ss.SSS') + 1,'yyyy-MM-dd HH:mm:ss.SSS')
But for substraction I am not getting the correct result using below
select from_unixtime(unix_timestamp('2016-11-06 01:00:00.000','yyyy-MM-dd HH:mm:ss.SSS'),'yyyy-MM-dd HH:mm:ss.SSS') , from_unixtime(unix_timestamp('2016-11-06 01:00:00.000','yyyy-MM-dd HH:mm:ss.SSS') - 1,'yyyy-MM-dd HH:mm:ss.SSS')
The subtract result which I am getting
2016-11-06 01:00:00.000 2016-11-06 01:59:59.000
But the expected result is
2016-11-06 01:00:00.000 2016-11-06 12:59:59.000
This worked for me:
from_unixtime(unix_timestamp(ref.rptg_end_ts,'yyyy-MM-dd HH:mm:ss') + (-1),'yyyy-MM-dd HH:mm:ss') ref.rptg_end_ts
"USA & Canada: DST Ends on Nov 6, 2016 ...
Most of the United States, Canada, and Mexico's northern border cities
end Daylight Saving Time (DST) on Sunday, November 6, 2016.
DST ends on Sun, Nov
6, 2016
Clocks will be set back 1 hour to standard time from 02:00 (2 am) to
01:00 (1 am), local time."
https://www.timeanddate.com/news/time/usa-canada-end-dst-2016.html
Related
I'm working with BigQuery and have a table that looks like:
YEAR
MONTH
DAY
timezone
local time
2015
6
24
America/Los Angeles
1930
Where local time is given by hhmm. I'm wondering if I can format this information into a timestamp column in SQL that yields time in UTC.
I know I can use `TO_TIMESTAMP` but that would involve concatenating all these columns as strings first. Is there any better way to do this? If I were to concatenate, I'm not sure how I would use timezone and then back out UTC.
You might consider below.
WITH sample_table AS (
SELECT 2015 year, 6 month, 24 day, 'America/Los_Angeles' timezone, 1930 local_time UNION ALL
SELECT 2015 year, 6 month, 24 day, 'America/Los_Angeles' timezone, 2400 local_time
)
SELECT TIMESTAMP_SECONDS(
UNIX_SECONDS(TIMESTAMP(DATE(year, month, day), timezone))
+ DIV(local_time, 100) * 3600 + MOD(local_time, 100) * 60
) utc
FROM sample_table;
Since TIME(24, 0, 0) is not a valid time format, the query converts datetime into unix seconds and get back to UTC with the time calculation in seconds.
Input calculates to invalid time: 24:00:00
Query results
I have a column which has values in string type like below:
31-Oct-2016 12:00 AM
31-May-2015 12:00 PM
I want to convert the above column values to timestamp in IMPALA. Tried with cast, to_timestamp and other ways , but it is either showing syntax error or Null as result.
Can you please suggest a solution
2nd Requirement
There is a column like below in string, I want it to be converted to timestamp alone.
31-Oct-2016 12:00
31-May-2015 12:00
please suggest a way, I'm new to Impala
Thanks in advance
You can use below code. Unfortunately, impala doesn't have am pm conversion capability, but you can use a little code to identify PM and add 12 hours to that to convert properly.
select
if (right('31-Oct-2016 02:09 PM',2)='PM',
to_timestamp('31-Oct-2016 02:09 PM','d-MMM-yyyy H:m') + interval 12 hours,
to_timestamp('31-Oct-2016 02:09 PM','d-MMM-yyyy H:m')
) ampm_timestamp
Second requirement -
Impala always expects 24hour hour format when it converts datetime. So, in your case for 12 AM scenario, we have to do some special logic like below.
First check if its 12 AM, then minus 12 hours, else check if its PM, then add 12 hours(which covers 12PM scenario) and finally if its any other AM, it simply converts to timestamp.
select
CASE WHEN right('31-Oct-2016 12:09 AM',2)='AM' AND RIGHT( SPLIT_PART('31-Oct-2016 12:09 AM',':',1),2)='12'
THEN to_timestamp('31-Oct-2016 12:09 AM','d-MMM-yyyy HH:mm') - interval 12 HOURS
ELSE CASE WHEN right('31-Oct-2016 12:09 AM',2)='PM'
THEN to_timestamp('31-Oct-2016 12:09 AM','d-MMM-yyyy HH:mm') + interval 12 HOURS
ELSE to_timestamp('31-Oct-2016 12:09 AM','d-MMM-yyyy HH:mm')
END END AMPM_TIMESTAMP
So I have this table where I've got two times for each line, but no date and need to get the interval between those two, all is fine when it's:
11:00:00 - 09:38:54
Returns: 01:21:06
As there's no dates, times are stored in "time without time zone" format.
The problem arises when the time enters the next day and the hour becomes 00h, as there's no date the interval will something absurd like -22:58:21
Example:
00:00:00 - 22:59:01
Returns: -22:59:01
00:00:00 - 22:44:06
Returns: -22:44:06
Is there anyway to make SQL understand 00:00:00 as 24:00:00 for the sake of math without date?
The hours only range between 8 and 0, and nothing from the previous day goes further than 0h30, a simple case for "00h" solves it, but I can't make SQL understand 00h as 24h so far. Any ideas?
Like:
select '24:00:00'::time - '22:59:01'::time;
?column?
----------
01:00:59
UPDATE
If the 00:00:00 is coming from somewhere you can't modify in place then:
select time_fld from time_test ;
time_fld
----------
00:00:00
01:30:00
select coalesce(nullif(time_fld, '00:00:00'::time), '24:00:00') from time_test;
coalesce
----------
24:00:00
01:30:00
I'm trying to project a column that subtracts an employee's start time from end time to get total hours worked.
Here's what I have:
select task_name "NAME", to_char(ses_start, 'DD-MON-RR') "DATE", to_char(ses_start,'HH:MI PM') "START", to_char(ses_end, 'HH:MI PM') "END",to_number(to_char(ses_end,'HH24:MI')) - to_number(to_char(ses_start,'HH24:MI')) "TOTAL"
The TOTAL column isn't working - is it even possible to do something like this? It worked up until I added that column, with output similar to:
TASK_NAME DATE START END
----------------- ------------ ---------- -----------
create flyers 26-MAR-16 02:00 PM 04:30 PM
update website 28-MAR-16 11:00 AM 01:00 PM
So now I want a final column (preferably in hours):
TOTAL
-----
2.5
2
or, at least in hours&minutes (this is what I was trying to do)...
TOTAL
-------
02:30
02:00
Any suggestions?
you can substract Date types in oracle, it gives you a decimal number which is the duration in days
select
task_name,
to_char(ses_start, 'DD-MON-RR') DATE,
to_char(ses_start,'HH:MI PM') START,
to_char(ses_end, 'HH:MI PM') END,
round((ses_end - ses_start) * 24, 2) DURATION_IN_HOUR
...
I have a table where I store the times as varchars:
Times
starttime
00:00
16:00
22:00
From this table I can convert the column to a date like this:
Select to_date(starttime,'hh24:mi') from times
This gives me the following:
01/03/2013 00:00:00
01/03/2013 16:00:00
01/03/2013 22:00:00
How can I change this query so I can prefix the time values with today's date so I get the following instead: (16/03/2013 is today's date)
16/03/2013 00:00:00
16/03/2013 16:00:00
16/03/2013 22:00:00
Thanks
to_date(to_char(sysdate, 'dd.mm.yyyy')||' '||starttime, 'dd.mm.yyyy hh24:mi')
You can add the difference between the current date and the start of the month. I prefer this to string operations as you stick with dates, but it doesn't make much difference.
You can use TRUNC() to work it out:
select to_date('09:00','hh24:mi') + ( trunc(sysdate) - trunc(sysdate, 'mm'))
from dual
SQL Fiddle
trunc(sysdate) is the earliest today and trunc(sysdate, 'mm') is the beginning of the month. Oracle's date arithmetic means that it returns a day difference between today and the beginning of the month; giving you the difference you need to add to your original TO_DATE().