Hive: Calculate exactly 1 year from date in format 'yyyy-MM-dd' string - sql

I need to calculate if has passed exactly 1 year or more from this date '2021-01-29', in HIVE.
So the result date must be in 'yyyy-MM-dd' format, and equal to '2022-01-29' or later. '2022-01-28' it's not correct answer.
It's possible to use date_add('2021-01-29', interval 1 year), if so, could someone explain how?
Thank you in advance.

In newer versions of Hive since 1.2.0 you can add interval to the date:
select date('2021-01-29') + interval 1 year
Result:
2022-01-29
For old version of hive use this recipe:
1 Year = 12 months. Add 12 months using add_months function:
select add_months('2021-01-29',12)
Result:
2022-01-29
If you want to add more than one year, multiply 12 by the number of years.

Related

How to check gaps in date ranges - sql

I have a table of months, and it has two columns of dates : start date, and end date. And I need your help to check the missing months in date ranges using a simple sql query.
The example below illustrates sample of data:
08/12/2018 - 06/01/2019
07/01/2019 - 05/02/2019
08/03/2019 - 05/04/2019
06/04/2019 - 05/05/2019
Expected result:
Missing months
06/02/2019 - 07/03/2019
06/05/2019 - 03/06/2019
Note that l’m using Hijri calendar not Gregorian calendar, so the first month in my example which is 08/12/2018 G refers to this date 01/04/1440 H in Hijri calendar , and its end date 06/01/2019 refers to 30/04/1440 H and so on.. Note that I used this date format DD/MM/YYYY and Oracle sql DB.
Any help is greatly appreciated.
Your expected result does not seem to match your sample data and explanation, but I understand that you want to exhibit the boundaries of intervals that are not adjacent. You could do this with lag():
select
end_date + interval 1 day start_gap,
lead_start_date - interval 1 day end_gap
from (
select
t.*,
lead(start_date) over(order by start_date) lead_start_date
from mytable t
) t
where lead_start_date > end_date + interval 1 day
This uses standard date arithmetic; the exact syntax may vary depending on your RDBMS.

How to retrieve the WeekofMonth for a given date in Hive

I have a date field in Hive 2018-06-10, from which i need to get WeekOfMonth
WEEKOFYEAR(order_time)
I need output for 2018-06-10 as 3 (which is 3rd week. assuming week starts from Sunday)
Is there any built in function in Hive to retrieve WeekofMonth. I couldn't find any. I tried below to convert based on minutes and seconds but
from_unixtime(unix_timestamp(CURRENT_DATE())+7200)
But the above is not giving correct value
For the week of the month, you can get the day part of the month and divide by 7.
select case
when DAYOFMONTH(order_time)%7 = 0
then DAYOFMONTH(order_time)/7
else DAYOFMONTH(order_time)/7 + 1
end
Also you can use date_format function:
select date_format('2018-06-10','W');
See more format patterns here: SimpleDateFormat

How to get how many days passed since start of this year?

I have a query which uses needs to know how many days passed since 1st of January in the current year.
Which means that if the query runs for example in:
2nd Jan 2017 than it should return 2 (as 2 days passed since 1st Jan
2017).
10th Feb 2016 than it should return 41 (as 41 days passed since 1st
Jan 2016).
basically it needs to take Current Year from Curent Date and count the days since 1/1/(Year).
i have the current year with: SELECT EXTRACT(year FROM CURRENT_DATE);
I created the 1st of Jan with:
select (SELECT EXTRACT(year FROM CURRENT_DATE)::text || '-01-01')::date
How do I get the difference from this date to Current_Date?
Basically this question can be Given two dates, how many days between them?
Something like age(timestamp '2016-01-01', timestamp '2016-06-15') isn't good because I need the result only in days. while age gives in years,months and days.
An easier approach may be to extract the day of year ("doy") field from the date:
db=> SELECT EXTRACT(DOY FROM CURRENT_DATE);
date_part
-----------
41
And if you need it as a number, you could just cast it:
db=> SELECT EXTRACT(DOY FROM CURRENT_DATE)::int;
date_part
-----------
41
Note: The result 41 was produced by running the query today, February 9th.
Given two dates, how many days between them
Just subtract one from the other.
In your case you could just round the current_date to the start of the year and subtract that from the current date:
select current_date - date_trunc('year', current_date)::date
The ::date cast is necessary to get the result as an integer, otherwise the result will be an interval.
Another solution is to use DATEDIFF
SELECT DATE_PART('day', now()::timestamp - '2016-01-01 00:00:00'::timestamp);

Teradata Change format of Week Number

I'm pretty new to SQL so I hope this isn't a dumb question, tried to google but couldn't find anything.
I'm summing sales of departments per week in SQL and am using TD_SYSFNLIB.WEEKNUMBER_OF_YEAR (trans_dt) to get the week number.
I think everything is working except I'd like to change the format of the weeks to the start date of the week, e.g. week 1 = 1/4/15
Also, i'm not sure how to handle the very first of the year week 0 since I think that should be grouped up with week 52 of last year.
The following date math trick should get you Beginning of Week as an actual date without having to join to the SYS_CALENDAR view or using a function:
SELECT CURRENT_DATE - ((CURRENT_DATE - DATE '0001-01-07) MOD 7) AS BOW;
Starting with TD14 there's NEXT_DAY which returns the following weekday, if you subtract 7 days you get the previous day:
next_day(trans_dt - 7, 'sunday')

Hive date function to achieve day of week

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...!!!