How to get total experience in terms of date object - date

I have a condition here in which I will have total experience in terms of month and year. For example, two drop down will be there for asking total number of experience in month and year. So if I am working from 1 Jan 2012, then I will write total experience as 3 year and 11 months. Now I have to convert this 3 year and 11 months into date format so that I can save this into database

You could use java.util.Calendar:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, month);
calendar.add(Calendar.YEAR, year);
Date date = calendar.getTime();
As a word of caution, the day field would be set to today's date. Check the intended behaviour if the current day is outside of the bounds for the target month. For example, setting the month to February when calendar has a day field of 30. It might be wise to set the day to a known, valid value for every month (eg: 1) before setting the month and year.

Use DATE_SUB() function:
Try this:
SELECT DATE_SUB(DATE_SUB(CURRENT_DATE(), INTERVAL 3 YEAR), INTERVAL 11 MONTH);

You can use mysql's date_sub() function or <date> - interval <expression> unit syntax to subtract an interval from a date.
select date_sub(curdate(),interval '3-11' YEAR_MONTH) as start_date
UPDATE:
Following the conversation between the OP and #eggyal, the OP need to replace the period in the incoming data with - and construct an insert statement as follows:
insert into mytable (...,join_date,...) values (...,date_sub(curdate(),interval '3-11' YEAR_MONTH),...)

Related

Tableau, Week to Date, Month To Date, Year to Date parameter

I have a data set spanning 2 years and is updated daily, created a dashboard to give a view of incidents by date group. I have created a parameter using date trunc for Day/Week/Month/Quarter/Year. This is in Tableau.
I am trying to get the parameter to show a Week to date, Month to date and so on view. IE if on Weds 15th Dec I selected the weekly view, it would only show data for every week in the data set for Sat-Weds (My weeks go Sat-Fri) or the monthly view every month between 1st-15th
Ideally I am wanting this as a simple parameter for a drop down menu, I can do the week to date stuff as a rolling sum restarting every week in a separate table, but for ease I just need a date group function that will allow to do this.
Any suggestions would be appreciated.
Cheers
Pete
The solution is 5 parts:
Date Part Parameter
Max date [Max Date]
Dynamic date part of the Max date [Dynamic Date Part Max]
Dynamic date part of the historical dates [Dynamic Date Part]
Filter those date parts <= the Max date [Dynamic Date - Lass than Max]
Date Part Parameter
Max Date
This is the calculation you'd use with your dataset to find the max date.
{ MAX([Order Date]) }
In order to create a good example, I'm going to set my Max date to a specific date the falls in the middle of a week, in the middle of a month and middle of the year. I'm going use June 13th, 2018 as my Max Date.
So, if you want to follow along you can use the below date as your max date. You can also use this data set if you'd like.
DATE(#2018-06-13#)
Dynamic date part of the Max date
DATEPART([Select Date Part], [Max Date])
Dynamic date part of the Historical dates
DATEPART([Select Date Part], [Order Date])
Filter on Historical dates parts <= the Max Date
[Dynamic Date Part] <= [Dynamic Date Part Max]
Now that we have all the pieces we need let's check to make sure they are working as we would expect.
Looks like we're seeing all the days of the month that are <= the 13th.
When we change it to Day of the Week we see only the days of the week <= the 4th day of the week which is Wednesday when the week starts on Saturday.
Now let's calculate the running sum of sales along our dynamic date part to better help you with your example.
Drag the measure you want to calculate the running sum onto the label, then create a quick table calculation. We'll next need to edit this table calculation as so.
You'll then see your calculation working as you would expect.
Hope this was helpful. Happy vizzing!

Tableau: Same Day Last Year Auto Filter

I am trying to compare yesterday's data to the same day the year before. For example, yesterday is 11 November 2018. I want to compare to 12 November 2017 (same day but the year before). I am wanting this to be applied automatically on the filter so all I need to do is open the file and verify the numbers are correct before sending off the report.
Any help would be appreciated.
Thanks
There are many Tableau functions that manipulate dates. A couple in particular are relevant to your problem:
Today() - returns the current date
DateAdd() - adds or subtracts an interval from a date. For instance, DateAdd('year', Today(), -1) gives the date one year prior to today. The first argument to DateAdd is the level of granularity or date part.
DateDiff() - determines the difference of the interval between two dates. DateDiff('day', [Start Date], [End Date]) returns the number of days separating the two date arguments.
The functions are well documented in the online help. Construct the formulas you need and filter accordingly.
Isolate yesterday's date as its own field. For instance if that is the max date in your data, then {max([Date])} would create an LOD of the maximum date.
Then make a calculation that will display the same date last year:
year([Date]) = year([max_date])-1
and datepart('week',[Date]) = datepart('week',[max_date])
and datepart('weekday',[Date]) = datepart('weekday',[max_date])

Counting weeks in Tableau (starting in specific month)

I need to count number of weeks in a fiscal year starting with Oct 1. My fiscal year goes from Oct 1st to September 30th.
When I apply:
ATTR(datepart('week',[Generation Date]))
It counts the weeks, but it starts in January. My data properties and default properties are set to October.
Instead of using inbuild week function you can create your own week number by just calculating date difference from fiscal year starting and then dividing by 7 then just take the next possible integer to get your week.
Let me know if you need exact formula
You should filter the dates by adding conditions using IF in a calculated field. In your case something like this if your fiscal year is fixed from Oct 1st to September 30th:
Write the following Formula in a Calculated field:
IF ([Generation Date] >= #01-10-2017#) AND [Generation Date] <= #30-09-2018#)
THEN [Generation Date]
END
Now use this calculated field the way you were using it before:
ATTR(DATEPART('week',[Calculated field]))

how would I query 4/1/ of the 72nd birth date year > an input date

I have tried the following:
add_months(to_Date('04/01/ind.birth_dte','MM/DD/YYYY'), 864) >= to_date('&StartDt','MM/DD/YYYY')
Is there a better way to pull April first of the participant's 72nd birth date?
You could use an interval calculation instead, but not sure how you're defining 'better'. Assuming you do want April 1st of the year in which their 72 birthday falls:
trunc(ind.birth_dte, 'YYYY') + interval '72-3' year to month
The trunc() function goes to the first day of their birth year, and the interval adds 72 years and 3 months to that, which will be April 1st.
SQL Fiddle with some sample dates, including a leap day to show that isn't a problem.
Or to compare that adjusted date with a fixed date as a filter:
where trunc(ind.birth_dte, 'YYYY') + interval '72-3' year to month
> to_date('&StartDt','MM/DD/YYYY');
SQL Fiddle.
You can use the trunc() method with your version as well to save building up a string and calling to_date, adding an additional three months to the add_months call (though I'd suggest you at least need a comment indicating where '867' comes from):
where add_months(trunc(ind.birth_dte, 'YYYY'), 867)
> to_date('&StartDt','MM/DD/YYYY');

SQL DateDiff Weeks - Need and alternative

The MS SQL DateDiff function counts the number of boundaries crossed when calculating the difference between two dates.
Unfortunately for me, that's not what I'm after. For instance, 1 June 2012 -> 30 June 2012 crosses 4 boundaries, but covers 5 weeks.
Is there an alternative query that I can run which will give me the number of weeks that a month intersects?
UPDATE
To try and clarify exactly what I'm after:
For any given month I need the number of weeks that intersect with that month.
Also, for the suggestion of just taking the datediff and adding one, that won't work. For instance February 2010 only intersects with 4 weeks. And the DateDiff calls returns 4, meaning that simply adding 1 would leave me the wrong number of weeks.
Beware: Proper Week calculation is generally trickier than you think!
If you use Datepart(week, aDate) you make a lot of assumptions about the concept 'week'.
Does the week start on Sunday or Monday? How do you deal with the transition between week 1 and week 5x. The actual number of weeks in a year is different depending on which week calculation rule you use (first4dayweek, weekOfJan1 etc.)
if you simply want to deal with differences you could use
DATEDIFF('s', firstDateTime, secondDateTime) > (7 * 86400 * numberOfWeeks)
if the first dateTime is at 2011-01-01 15:43:22 then the difference is 5 weeks after 2011-02-05 15:43:22
EDIT: Actually, according to this post: Wrong week number using DATEPART in SQL Server
You can now use Datepart(isoww, aDate) to get ISO 8601 week number. I knew that week was broken but not that there was now a fix. Cool!
THIS WORKS if you are using monday as the first day of the week
set language = british
select datepart(ww, #endofMonthDate) -
datepart(ww, #startofMonthDate) + 1
Datepart is language sensistive. By setting language to british you make monday the first day of the week.
This returns the correct values for feburary 2010 and june 2012! (because of monday as opposed to sunday is the first day of the week).
It also seems to return correct number of weeks for january and december (regardless of year). The isoww parameter uses monday as the first day of the week, but it causes january to sometimes start in week 52/53 and december to sometimes end in week 1 (which would make your select statement more complex)
SET DATEFIRST is important when counting weeks. To check what you have you can use select ##datefirst. ##datefirst=7 means that first day of week is sunday.
set datefirst 7
declare #FromDate datetime = '20100201'
declare #ToDate datetime = '20100228'
select datepart(week, #ToDate) - datepart(week, #FromDate) + 1
Result is 5 because Sunday 28/2 - 2010 is the first day of the fifth week.
If you want to base your week calculations on first day of week is Monday you need to do this instead.
set datefirst 1
declare #FromDate datetime = '20100201'
declare #ToDate datetime = '20100228'
select datepart(week, #ToDate) - datepart(week, #FromDate) + 1
Result is 4.