Qlikview - subtract one year - qlikview

I want in text box to do next:
Today() - one year
and get
Today = 20.9.2016
Past = 20.9.2015
Any idea?

just use AddYears(startdate, n)

You can use AddMonths() function and add -12 months:
Today() // result: 20/09/2016
AddMonths( Today(), -12 ) // result: 20/09/2015
More about AddMonths() function you can read here

Related

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

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.

Getting data that précéde the today's date with inteval of two years

I have file with a column dates.
I want to get the dates which precede the current date with interval of two years.
I mean; we are 18/08/2021: I want all dates before 18/08/2019. of course with a dynamic date.
so tomorrow i will get all the dates before 19/08/2019
You can achieve this with a DAX measure as following
Include/Exclude:=
VAR _1 =
DIVIDE(CONVERT ( TODAY () - MAX ( tbl[Dates] ), INTEGER )-1, 365)
VAR _2 =
SWITCH ( TRUE (), _1 > 2, "Include", "Exclude" )
RETURN
_2
Do you simply want a where clause?
where datecol < dateadd(year, -2, convert(date, getdate()))

Hive SQL Integer YYYYMM previous Months

I want to create an SQL statement that looks for the last 2 months.
For example:
Select *
from x
where sampledate<= YYYYMM-2
currently i am using this:
(year(from_unixtime(unix_timestamp()))*100+month(from_unixtime(unix_timestamp())))-1
but it returns wrong statements for the first 2 months of a year :(
My idea is to calculate with a date and then change it to a yyyymm integer format.
Any ideas?
Could you try this:
SELECT colomn
FROM table
WHERE date > (SELECT add_months(from_unixtime(unix_timestamp()),-2));
or you can use:
SELECT colomn
FROM table
WHERE date > to_date(SELECT year(add_months(from_unixtime(unix_timestamp()),-2))+month(add_months(from_unixtime(unix_timestamp()),-2)));
Combined with regex&substring:
SELECT colomn
FROM table
where sampledate>=substr(regexp_replace(add_months(from_unixtime(unix_timestamp()),-2), '-',''),1,6)
to get a YYYYMM date
If you want to avoid converting an integer, in YYYYMM format, to and from a date, you can just use maths and CASE statements...
For example YYYYMM % 100 will give you MM. Then you can check if it's 2 or less. If it is 2 or less, deduct 100 to reduce by a year, and add 12 to get the month as 13 or 14. Then, deducting 2 will give you the right answer.
Re-arranging that, you get YYYYMM - 2 + (88, if the month is 1 or 2)
sampledate <= YYYYMM - 2 + CASE WHEN YYYYMM % 100 <= 2 THEN 88 ELSE 0 END
The better idea may just be to reshape your data so that you actually have a (real) date field, and just use ADD_MONTHS(aRealDate, -2)...
EDIT:
If your actual issue is generating the YYYYMM value for "two months ago", then deduct the 2 months before you use the YEAR() and MONTH() functions.
year( ADD_MONTHS(from_unixtime(unix_timestamp()), -2) )*100
+
month( ADD_MONTHS(from_unixtime(unix_timestamp()), -2) )
Try something like this.
First, a utility to get the date n months in the future/past:
public Date nMonthsFromDate(Date date, int interval) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// E.G. to get 2 months ago, add -2
cal.add(Calendar.MONTH, interval);
Date result = cal.getTime();
return result;
}
Criteria query on the entity, here Member:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Member> q = cb.createQuery(Member.class);
Root<Member> memberRoot = q.from(Member.class);
Date theDate = nMonthsFromToday(-2);
Predicate pred = cb.greaterThanOrEqualTo(
memberRoot.<Date>get("timeStamp"), theDate);
q.where(pred);
TypedQuery<Member> theQuery = em.createQuery(q);
String qStr = theQuery
.unwrap(org.apache.openjpa.persistence.QueryImpl.class)
.getQueryString();
LOG.info("Query: " + qStr);
List<Member> results = null;
try {
results = theQuery.getResultList();
} catch (Exception e) {
LOG.severe(e.getMessage());
e.printStackTrace();
}
return results;
Finally, beware of comparing a date [java.util.Date] to a timestamp [util.sql.Date]. Due to a quirk in Java, for equivalent dates, date.equals(timeStamp) returns true, BUT timeStamp.equals(date) returns FALSE. To conform both dates to a java.util.Date:
public java.util.Date getStandardDate(Date date) {
return new java.util.Date(date.getTime());

return week number based on date

I am new to T-SQL and needed urgent assistance here.
I am trying to get the week number from a given date.
I understand that there is a build in function for it but the value return is not exactly what I wanted.
For e.g., by using select datepart(wk, '2013-01-07'), it would return me '2'.. but the actually fact is it should return '1' instead of '2'.
Any ideas how to correct this issue?
You can use dy datepart specifier to get dayOfYear number and divide it by 7:
select (datepart(dy, '2013-01-05') - 1) / 7 + 1;
Working DEMO.
Try this
SELECT DATEPART(WEEK,GETDATE())
This depends on hop you define the first week. Does it always start on the same weekday? or does it always start on the first of January? If you want it to always start on the same weekday, then use Set datefirst to tell T-SQL what weekdaty you want to define as the start of the week. If you want it to always start on Jan 1, then just use day of year instead of week, subtract 1, integer divide by 7 and add 1.
declare #dat DateTime = getdate()
Select Select (datepart(dy, #dat)-1) / 7 + 1
Although going from memory, I believe the ISO standard for the first week of the year is the week in the year that the first Thursday of the year is in. This would possibly explain why the built in function gives a result different to that you require.

SQLite - Negative value by substracting months of different years

I am trying to retrieve the month value of a calculation :
SELECT strftime('%m', 'now') - strftime('%m', e.date) AS something FROM ...
But here are the results I get and what I really want :
NOW - 2012-02-03 = 0 // want 11
NOW - 2012-11-02 = -9 // want 3
NOW - 2012-02-02 = 0 // want 12
NOW - 2012-01-02 = 1 // want 13
As I can see I can almost get the right values by doing sqlResult + 12 (except for first example) but is there a way to achieve this directly in the SQL statement and to get exact values in every case ?
EDIT : Finally here is the solution
SELECT CAST ((julianday('now') - julianday(e.date_retour)) / 30 AS integer) AS something
You need to take the year into account. You can do this as:
select strftime('%Y', 'now')*12+strftime('%m', 'now') -
(strftime('%Y', e.date)*12+strftime('%m', e.date))
The month returns the month of the year. So, it is no surprise that something like 2 (February) - 9 (September) produces a negative number.
To handle day of month, I think you have to handle the date arithmetic yourself:
select (strftime('%Y', 'now')*12+strftime('%m', 'now') -
(strftime('%Y', e.date)*12+strftime('%m', e.date))
) -
(case when strftime('%d', 'now') < strftime('%d', e.date) then 0 else 1 end)
SQLite has the ability to add months to a date. Unfortunately, it doesn't seem to have the ability to take the difference in months between dates.
You are not really looking at difference between two months but, considering their years as well.
So you need to do like this:
SQLite (SQL.js) Demo
SELECT round((julianday(Date('now')) -
julianday(Dates))/30) as MonthsDiff
from demo;
| MonthsDiff |
--------------
| 11.0 |
| 2.0 |
you are getting the weird numbers because you are asking the strftime function to return only the month and then you are subtracting only the months...
How about you do this?
SELECT strftime('%s', date()) - strftime('%s', '2012-03-03') as blah
this will subtract the unix timestamps and then you can convert it back to readable months or whatever...
or this could give you number of days...
SELECT julianday(date()) - julianday('2012-03-03') as blah