I have an array with dates in ascending order, I am trying to create a Snowflake UDF to count the array element between two dates.
The function should get the two days and tell us the count between the two days.
create or replace function workdays_between(date1 String, date2 String)
RETURNS double
LANGUAGE JAVASCRIPT
as
$$
var DATE1 = DATE1
var DATE2 = DATE2
var count = 1
var i = 1
a = ["2017-01-01","2017-01-02","2017-01-03","2017-01-06","2017-01-09","2017-01-10"]
if (DATE1 < DATE2) {
for (i=1;a[i]<=DATE2;i++) {
if (a[i] >= DATE1) {
count = count + 1;
}
}
}
else
{
for (i=1;a[i]<=DATE1;i++) {
if (a[i] >= DATE2) {
count = count + 1;
}
}
count = count * -1;
}
return count;
$$
But I am not getting the desired output, please help.
the problem was with the case on DATE1 and DATE2, I gave in lower case inside the program, it worked after making it to upper case.