Convert julian date in Oracle SQL null - date

I want to convert Julian dates to normal dates (from JDE database). I run this query:
select to_date(to_char((DATE+1900000)),'YYYYDDD') from table ;
And I get the following error:
ORA-01848: day of year must be between 1 and 365 (366 for leap year).
I know it's because some dates are null or have no values.
Can anyone help me out on this please?

I have replicated your query and I tried with different values. Only if the date value is "0", you will get this error:
ORA-01848: "day of year must be between 1 and 365 (366 for leap year)"
So I thought that maybe you were interested in avoid the "0" values, and you can avoid it with a simple where, like this one:
SELECT to_date(to_char((DATE+1900000)),'YYYYDDD')
FROM table
WHERE DATE != 0;

Related

How do I filter my data for the same time last year - Power Query

I'm making an excel file that will be used for a report on sickness absence.
We are looking at the last 100 days (which I've been able to do with a simple filter in my Applied Steps) and we want to compare to the same time last year plus 100 days before that date AND look at 50 days after that date.
How can I filter for this in Power Query? Or do I have to write a custom formula? (In which case, does anyone know what and where to write it)
Idea is to be able to run this report every day/week and give updates on how we're doing in the winter in comparison to last year.
Any help on this will be greatly appreciated, thank you in advance!
Use the drop down filter atop the date column ... Date filters ... between ... and enter some random set of dates like 1/30/2019 through 1/30/2020
That generates this code
= Table.SelectRows(#"Changed Type", each [date] >= #date(2019, 1, 30) and [date] <= #date(2020, 1, 30))
edit it for your desired date range which could be like this
= Table.SelectRows(#"Changed Type", each [date] >= Date.AddDays(targetdate, -465) and [date] <= Date.AddDays(targetdate, -315))
I assume -465 which is last year (365 days prior) plus 100 days back, and -315 which is -365+50
As for what to use instead of targetdate ... you didn't tell us .. perhaps hardcode a date #date(2019,10,31), use whatever you had in your original filter, or maybe the maximum of the current date column
List.Max(#"priorstepname"[date])

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])

Extract highest date per month from a list of dates

I have a date column which I am trying to query to return only the largest date per month.
What I currently have, albeit very simple, returns 99% of what I am looking for. For example, If I list the column in ascending order the first entry is 2016-10-17 and ranges up to 2017-10-06.
A point to note is that the last day of every month may not be present in the data, so I'm really just looking to pull back whatever is the "largest" date present for any existing month.
The query I'm running at the moment looks like
SELECT MAX(date_col)
FROM schema_name.table_name
WHERE <condition1>
AND <condition2>
GROUP BY EXTRACT (MONTH FROM date_col)
ORDER BY max;
This does actually return most of what I'm looking for - what I'm actually getting back is
"2016-11-30"
"2016-12-30"
"2017-01-31"
"2017-02-28"
"2017-03-31"
"2017-04-28"
"2017-05-31"
"2017-06-30"
"2017-07-31"
"2017-08-31"
"2017-09-29"
"2017-10-06"
which are indeed the maximal values present for every month in the column. However, the result set doesn't seem to include the maximum date value from October 2016 (The first months worth of data in the column). There are multiple values in the column for that month, ranging up to 2016-10-31.
If anyone could point out why the max value for this month isn't being returned, I'd much appreciate it.
You are grouping by month (1 to 12) rather than by month and year. Since 2017-10-06 is greater than any day in October 2016, that's what you get for the "October" group.
You should
GROUP BY date_trunc('month', date_col)

How to get total experience in terms of date object

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),...)

SQL query to find date between a range of dates

Table X has start_date and end_date and related information associated with these dates. BOTH stored in DB in date format.
I need to find a specific date say 12th-Jan-2000 and extract the rows whose date range includes this date.
Could someone please help me out. I am new to SQL so need some guidance here.
Table X:
ID |start_date|end_date
1 |12/30/1999|01/12/2000
2 |01/20/2000|01/30/2000
3 |01/07/2000|01/15/2000
Thus my query should give back the ID-3 since 12th January falls in the range 01/07/2000-01/15/2000
Thanks
use the BETWEEN operator:
SELECT *
FROM TableX
WHERE DATE'2000-01-12' BETWEEN start_date AND end_date