I am working on Snowflake, need to substract 2 hours from specifc date:
date time: 2021-06-10 14:07:04.848 -0400
'2021-06-10 14:07:04.848 -0400' - 2 hours
expected result: 2021-06-10 12:07:04.848 -0400 (now it's twelve o'clock).
Datediff didn't work:
DATEDIFF(hour,2,TO_DATE(substr(p.insertedon,1,10)))
There is any simple way to do this? just a subtraction of 2 hours to date time
Regards
Using INTERVAL:
SELECT p.insertedon - INTERVAL '2 HOURS'
FROM ...
Related
I want a query that shows a time difference in months or days in Impala
How can I do this?
start 2017-11-29 19:45:00 - end 2018-11-29 21:30:00
I know that month_between and datediff shows the month of datediff but how do I make it so it also takes the year into count when counting the days / months?
For the above example, I want to to display either
month_between - 12.2 months - equivalent to the month calculation of the timestamp - might be a little off cause I did it by hand / 30 days
days_between - 366 days
not sure if you tried DATEDIFF or not , but it already gives you the dates difference in days :
select datediff(endddaate, startdate)
from tablename
I want a query that shows a time difference in hours and minutes in Impala. Or in SQL?
How can I do this?
start 2017-11-29 19:45:00 - end 2017-11-29 21:30:00
So DATEDIFF gives you the difference between two dates in DAYS. This can be converted to hours by multiplying by 24. The remainder of this answer is then minutes if multiplied by 60.
Try use
secondsDiff = timestamp2 - timestamp1
minutes_diff = secondsDiff / 60
hours_diff = secondsDiff / (60*60)
I calculate the day difference between 2 dates with date_part
DATE_PART('day', '2017-11-17 13:54:15' - '2017-11-12 18:05:18')
The day difference in here is 5 days when calculated.
However the sql returns as 4 since for the last day it has not been 24 hours.
But I would like to count it as 1 day.
How can I do this?
Here is one method:
DATE_PART('day'
date_trunc('day', '2017-11-17 13:54:15'::timestamp) - date_trunc('day', '2017-11-12 18:05:18'::timestamp)
)
I'm looking for a workaround or hive date functions that gives day of the week ,
Sunday - 1
Monday - 2
Tuesday - 3
Wednesday - 4
Thursday - 5
Friday - 6
Saturday - 7
Requirement in detail : I'm looking for a function that takes date string (YYYYMMDD) as input and outputs the day of the week as per the above table.
Consider using from_unixtime(your date,'u') - this will return day number of week starting from Monday=1.
If your date is not in unixtime format, you can use the following instead:
from_unixtime(unix_timestamp('20140112','yyyyMMdd'),'u')
see: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html for simple date format documentation.
You can now use date_format (Hive 1.2):
hive> select date_format('2016-12-01' ,'u');
OK
4
select pmod(datediff(your_date,'1900-01-07'),7) + 1 as WeekDay from your_table
arbitrary start date picked (1900-01-07)
calculates the mod 7 day of week (plus 1 to start at 1 instead of zero)
Expanding on iggy's answer, here is the query to get the days of the week. Adjust the query to set the first day of the week as necessary.
SELECT current_date AS `Date`,
CASE date_format(current_date,'u')
WHEN 1 THEN 'Mon'
WHEN 2 THEN 'Tues'
WHEN 3 THEN 'Wed'
WHEN 4 THEN 'Thu'
WHEN 5 THEN 'Fri'
WHEN 6 THEN 'Sat'
WHEN 7 THEN 'Sun'
END AS day_of_week
From Hive 2.2 there is another possibility:
hive> select extract(dayofweek FROM your_date) FROM your_table;
As I said you need to write a UDF which will accept a string as parameter and return a string.
Inside the UDF you need to do these steps:
1.) Parse the input string using SimpleDateFormat(YYYYMMDD)
2.) Use the Below code to get the day of week:
Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
3.) Use this dayOfWeek value in a case statement to get your weekday String and return that string.
Hope this helps...!!!
I need to check if any work time period is between datetime and calculate hours between for work hours sum grater than 8 hours.
Sample data:
1. Work time: 07:00 - 17:00 and datetime for checking 06:00-22:00
Answer: 2 hour
2. Work time: 13:00 - 23:00 and datetime for checking 06:00-22:00
Answer: 1 hour (only 1 hour is grater 8 hours and between 06:00-22:00 )
3. Work time: 19:00 - 05:00 and datetime for checking 22:00-06:00
Answer: 2 hour (only 2 hour is grater 8 hours and between 22:00-06:00 )
Any ideas?
Try this: T-SQL DateDiff - partition by "full hours ago", rather than "times minutes turned 00 since"
Basically, DATEDIFF(HOUR, endTime, startTime). Then, subtract your result from 8 to get the difference (if they were "under" time, the difference will be negative).