How to add years to date in Hiveql - hiveql

Hive query equivalent of T-SQL Statement SELECT DATEADD(year, 1, '2019/05/25') ?
I am aware add_months() works for adding months to a given date. Now i am looking for adding years to a date.
Thanks

Try the functions years_add(timestamp date, int years) or years_sub(timestamp date, int years) which are mentioned in cloudera's latest documentation.
If this solutions doesn't work, you can still try using add_months() and multiply your desired number of years by 12 to add the correct number of months.
Hope this helps !

Related

MDX Get Sales starting from 2 years ago to today

I'm trying to make a dynamic report that pull sales starting from the first day of the fiscal year 2 years ago up to today and rolls forward with each new fiscal year. Our fiscal years don't line up with calendar years.
I have little MDX experience and am still learning.
So it should look at todays date, get the current fiscal year, subtract 2 years from it and then pull sales starting from that year up to today.
I had some difficulty just trying to get the date working correctly as I was getting errors however the below query now pulls yesterdays sales for me. I assume I need to reference [Date].[Year] as well, but I don't know how to use it to get my desired results.
SELECT
NON EMPTY
{ [Measures].[Gross Margin Percentage],
[Measures].[Gross Margin Value],
[Measures].[Sales Value],
[Measures].[Sales Units] }
ON COLUMNS
FROM IMR
Where
{StrToMember("[Date].[Date].&" + Format(CDate(now()-1), "[yyyy-MM-ddT00:00:00]"))}
If you'd like the results to be across a range of dates then try StrToSet in your WHERE clause and construct in a similar way to what you are doing.
You currently have this:
Where
{StrToMember("[Date].[Date].&" + Format(CDate(now()-1), "[yyyy-MM-ddT00:00:00]"))}
Here is an example for 2 days which you can adapt to your needs:
Where
{StrToSet(
"[Date].[Date].&" + Format(CDate(now()-3), "[yyyy-MM-ddT00:00:00]")
+ ":"
"[Date].[Date].&" + Format(CDate(now()-1), "[yyyy-MM-ddT00:00:00]")
)}

How to get network days by subtracting from current date and older date on Jasper soft studio? [duplicate]

This question already has answers here:
Calculating Time and Date difference
(1 answer)
How to create a single expression displaying time difference between two Date's as years, months, days, hours, minutes, seconds
(1 answer)
Closed 2 years ago.
I need to find net workdays by subtracting from older date from today ?
Ex:Today Date is 7/14/2020 and older date is some 7/14/2019.
Expected result = 365.
The expression is extract(days from now() - <older date column>).
My recommendation is to have the database server perform this calculation for you by adding the expression to the select list in your query. If you prefer to do this with scripting after Jaspersoft has retrieved the data, then I cannot help you there.

How to convert a 5 digit INT to date

I apologise if this question has been asked but I cannot find an answer that quite fits.
I have a database with dates stored as 5 digit integers. I can covert these to datetime, however the dates are showing in the future.
For example,
select convert(datetime,StartDate,103)
from dpm.Schedule
where ScheduleID like 50003;
Gives the results of
2107-05-31 00:00:00:000 but this date should actually be 26/05/2008.
I am pretty new to T-SQL and have looked for sometime to find the answer to this but I am reaching the end of my sanity.
We cannot answer because you are doing a lot of confusion. To start 5003 is the Id of the record and [StartDate] is the value you are trying to convert.
Also drop using operator LIKE in the Id
Example:
select convert(datetime,50003,103)
returns:
2036-11-26 00:00:00.000
but you are trying to convert the value returned by
select StartDate
from dpm.Schedule
where ScheduleID = 50003;
Please edit your question and show us a nice example using SQL Fiddle

Hive: Subtracting 1 year from current date

I'm trying to find the best way to subtract 1 year and also one month from the current date in a Hive query. Using the following, I don't believe it will take into account leap years or if the fact that months have different amounts of days so eventually the code will break. Any help would be greatly appreciated!
set my_date = from_unixtime(unix_timestamp()-365*60*60*24, 'yyyy-MM-dd');
set my_date = from_unixtime(unix_timestamp()-30*60*60*24, 'yyyy-MM-dd');
Thank!
-Rebecca
If you have date format like yyyy-MM-dd hh:mm:ss in Hive, it is easy to implement using following functions
concat((year(date_field)-1),'-', (month(date_field)-1), '-', day(date_field))
Use IF and CASE functions to implement your logic to find whether it is a leap year or not(by dividing year by 4)

Difference in Days in DB2

I have to calculate the difference in days between two dates and I figured out that
there is no such thing as a DATEDIFF() function in DB2.
I tried doing it like that:
(DAYOFYEAR(date1)-DAYOFYEAR(date2)+(YEAR(date1)-YEAR(date2))*365)
This is obviously not working for leap years, but I do not have to deal with that.
I know that date1 is always later than date2.
Do I have any flaws in my logic? It is not working (it's an exercise and I have a function to test my results). Is there an easier way to do that?
Thank you.
It would possibly help if we knew what version of DB2 you were using and what platform it was running on. But it seems likely that you can do something like this:
select
days(my1stdate) - days(my2nddate) as myDuration
from mySchema.myTable
The DAYS() function converts a DATE value into the number of days between Jan 1, 0001, and the supplied DATE value. Once both DATEs are converted, the subtraction (difference) is straightforward.