to calculate number of days between two dates in dql documentum - documentum

I have two attributes, effect_date,next_effect_date. I have to calculate the difference between these two dates in days. what will be the dql query? Please Help

Use this query for dm_document and date properties r_creation_date and r_modify_date which are generic object type & attributes:
select DATEDIFF(day, r_creation_date, r_modify_date)
from dm_document where r_object_id = '<set_id_here>'
When you specify object type name we can adjust this query.
Syntax for DATEDIFF function is DATEDIFF(date_part, date1, date2)
Result value depends on DB under your repository. From documentation:
If the repository is using Oracle or DB2, the return value is a floating point number.
If the repository is using DB2, the server assumes 365 days per year and 30.42 days per month (the
mean number of days in a month). These assumptions can cause return values that differ from the
expected value. To illustrate, the following example, which asks for the number of days between
March 1, 1996 and Feb 1, 1996, returns 30.42 instead of 28:
DATEDIFF(day, date('03/01/1996 0:0:0'),
date('02/01/1996 0:0:0'))
If the repository is using MS SQL Server, the return value is an integer for all units except day. If you
specify day in the function, the return value is a floating point.
The MS SQL Server implementation round up if the difference is one half or greater of the specified
unit. The implementations round down if the difference is less than one half of the specified unit. To
illustrate, the following example, which asks for the difference between March 1, 1996 and July 1,
1996 expressed as years, returns 0 because the difference is only 4 months.
DATEDIFF(year,date('03/01/1996'),date('07/01/1996'))

Related

Apache Lucene ignores months and days in date range search expression

I try to query Apache Lucene index which is built using a database table that contains a date column and my query refers to this very column. In Luke the search expression I use is as follows:
column_name:[yyyy-MM-dd TO yyyy-MM-dd]
The results returned are records which do not have dates (in the queried column) with the year in the start value or older and the ones that do have year after the year in the start value up to the year in the end value. So if I write column_name:[2011-05-22 TO 2015-09-03] - I will get records with the dates in the column with years 2012, 2013, 2014, 2015.
However, the results will not be precise according to the search expression - the month and days value will be ignored. No matter what month and day I will set up - the search will result in records being returned with dates across each of the searched year (from 01.01 to 31.12).
The issue occurs if I use another date format like:
column_name:[yyyyMMdd TO yyyyMMdd]
I am looking to find what might be causing this? Is this a matter of date formatting while indexing? Or is this a matter of date format in the search expression?
I should add for more clarity that the search expression:
column_name:"yyyy-MM-dd"
will return records as exepcted - so with the date included in the expression.
Analysis is the issue.
When you index, your dates are getting split into three terms: "2011", "05", and "22". Any one of the terms can be matched against a range query, but not all three. So for [2011-05-22 TO 2015-09-03], the term 2011 does not fall lexicographically in that range, while 2012, 2013, 2014 and 2015 all do. For another example, if you searched for [1999-01-01 TO 2000-01-01], you would also find all dates on the twentieth of a month, because 20 falls within that lexicographic range.
Since this field is just a date, you should just avoid tokenizing it. Use StringField instead of a TextField, and re-index.

How do I get the number of days in a month for the specific date in that row of data using SQL?

For example, if I have a data set including two columns, one which shows the month as a number and the other which shows the year (result of grouping my data using GROUP BY), I want to add another column called 'Days in the month' which will display the number of days in the respective month. Is there a way I can do this? Is there some function I can add in the SELECT clause?
I want to do this since there are further calculations I need to do with that number for each row.
In SQL Server 2012+, you can use:
select day(eomonth(datecol))
eomonth() gets the last day of the month. day() just returns the day of the month -- the number of days in the month, in this case.
For older SQL Server versions, I use the following:
DAY(DATEADD(MONTH, DATEDIFF(MONTH, -1, date_column)- 1, -1))
Much less elegant than the previous answer, but functional.

SQL ORACLE Get week numbers from multiple datetime rows

I have 70.000 rows of data, including a date time column (YYYY-MM-DD HH24-MM-SS.).
I want to split this data into 3 separate columns; Hour, day and Week number.
The date time column name is 'REGISTRATIONDATE' from the table 'CONTRACTS'.
This is what I have so far for the day and hour columns:
SELECT substr(REGISTRATIONDATE, 0, 10) AS "Date",
substr(REGISTRATIONDATE, 11, 9) AS "Hour"
FROM CONTRACTS;
I have seen the options to get a week number for specific dates, this assignment concerns 70.000 dates so this is not an option.
You (the OP) still have to explain what week number to assign to the first few days in a year, until the first Monday of the year. Do you assign a week number for the prior calendar year? In a Comment I asked about January 1, 2017, as an example; that was a Sunday. The week from January 2 to January 8 of 2017 is "week 1" according to your definition; what week number do you assign to Sunday, January 1, 2017?
The straightforward calculation below assigns to it week number 0. Other than that, the computation is trivial.
Notes: To find the Monday of the week for any given date dt, we can use trunc(dt, 'iw'). iw stands for ISO Week, standard week which starts on Monday and ends on Sunday.
Then: To find the first Monday of the year, we can start with the date January 7 and ask for the Monday of the week in which January 7 falls. (I won't explain that one - it's easy logic and it has nothing to do with programming.)
To input a fixed date, the best way is with the date literal syntax: date '2017-01-07' for January 7. Please check the Oracle documentation for "date literals" if you are not familiar with it.
So: to find the week number for any date dt, compute
1 + ( trunc(dt, 'iw') - trunc(date '2017-01-07', 'iw') ) / 7
This formula finds the Monday of the ISO Week of dt and subtracts the first Monday of the year - using Oracle date arithmetic, where the difference between two dates is the number of days between them. So to find the number of weeks we divide by 7; and to have the first Monday be assigned the number 1, instead of 0, we need to add 1 to the result of dividing by 7.
The other issue you will have to address is to convert your strings into dates. The best solution would be to fix the data model itself (change the data type of the column so that it is DATE instead of VARCHAR2); then all the bits of data you need could be extracted more easily, you would make sure you don't have dates like '2017-02-29 12:30:00' in your data (currently, if you do, you will have a very hard time making any date calculations work), queries will be a lot faster, etc. Anyway, that's an entirely different issue so I'll leave it out of this discussion.
Assuming your REGISTRATIONDATE if formatted as 'MM/DD/YYYY'
the simples (and the faster ) query is based ond to to_char(to_date(REGISTRATIONDATE,'MM/DD/YYYY'),'WW')
(otherwise convert you column in a proper date and perform the conversio to week number)
SELECT substr(REGISTRATIONDATE, 0, 10) AS "Date",
substr(REGISTRATIONDATE, 11, 9) AS "Hour",
to_char(to_date(REGISTRATIONDATE,'MM/DD/YYYY'),'WW') as "Week"
FROM CONTRACTS;
This is messy, but it looks like it works:
to_char(
to_date(RegistrationDate,'YYYY-MM-DD HH24-MI-SS') +
to_number(to_char(trunc(to_date(RegistrationDate,'YYYY-MM-DD HH24-MI-SS'),'YEAR'),'D'))
- 2,
'WW')
On the outside you have the solution previous given by others but using the correct date format. In the middle there is an adjustment of a certain number of days to adjust for where the 1st Jan falls. The trunc part gets the first of Jan from the date, the 'D' gets the weekday of 1st Jan. Since 1 represents Sunday, we have to use -2 to get what we need.
EDIT: I may delete this answer later, but it looks to me that the one from #mathguy is the best. See also the comments on that answer for how to extend to a general solution.
But first you need to:
Decide what to do dates in Jan before the first Monday, and
Resolve the underlying problems in the date which prevent it being converted to dates.
On point 1, if assigning week 0 is not acceptable (you want week 52/53) it gets a bit more complicated, but we'll still be able to help.
As I see it, on point 2, either there is something systematically wrong (perhaps they are timestamps and include fractions of a second) or there are isolated cases of invalid data.
Either the length, or the format, or the specific values don't compute. The error message you got suggests that at least some of the data is "too long", and the code in my comment should help you locate that.

SQL server: find last three years customer details

I have a table accountmaster.
when given a date,it should return number of customers enrolled(DOE) in last three years of the given date.
here is my query, which is not returning any values.plz help me with changes .TIA
select count(ACID) as numberofcustomers from ACCOUNTMASTER
where
datepart(yy,doe)>= datepart(yy,dateadd(yy,-2,'2012/04/10'))
and
doe<= datepart(yy,'2012/04/10')
Your issue is an apparent misunderstanding on what DATEPART does, and how to compare dates. So let us break down your WHERE clause.
where
datepart(yy,doe)>= datepart(yy,dateadd(yy,-2,'2012/04/10'))
This clause compares the year value of the date in column doe with the year of a date two years prior to a fixed date, representing the date submitted by the user. So, datepart(yy, dateadd(yy, -2,'2012/04/10')) returns a value of 2010 as an int. This is compared with the datepart(yy,doe), which returns the integer year of the date value stored in the doe column. Since these two data types are both int, the compare does what it should and returns information from records dated 2010/01/01 and later. No problems here - unless you are expecting to return data from 2010/04/10 forward, in which case you want to compare the value dateadd(yy, -2,'2012/04/10') to the value in the doe column.
and doe<= datepart(yy,'2012/04/10')
In this case, there is a comparison issue. You are attempting to compare the integer value 2012 with the date value in the doe column. Since these are not the same data type, we need to find out what is being compared.
I assume that the doe column is a datetime data type (if it was a date or datetime2, you would be getting an error.) An integer can be compared to a datetime, since SQL Server uses a numeric datatype to represent the datetime data type. A value of 2012 represents the date 1905/07/06 (use CAST(2012 as datetime) to see for yourself), so the WHERE clause translates out to something like:
WHERE datepart(year, doe) >= 2010
AND doe < '1905/07/06'
Since a date can not be both before July, 1905 and on or after Jan, 2010, no data is returned.
What is actually needed here depends on whether or not you only want to compare years, or you want to compare year/month/day. For years only, you can replace the last doe with DATEPART(year, doe) and have a functional query, albeit not a fast one. To compare year/month/day, use the DATEADD as mentioned above, and just use the actual entered date in the final comparison.
Hopefully, that helps.

How to get the difference between two dates (informix)?

How to get the difference between two dates (informix) in integer format like that
day = 15
mon = 2
year = 1
There are two sets of date/time values in Informix: DATE and DATETIME.
The DATE type is oldest (it was in the precursor to the SQL-based Informix), and represents the integer number of days since a reference date (where day 0 is 1899-12-31, so day 1 was 1900-01-01).
You get the difference between two DATE values in days by subtracting one from the other.
The DATETIME system is newer (but still old — circa 1990). You can take the difference between two DATETIME YEAR TO DAY values and get a result that is an INTERVAL DAY TO DAY (essentially the number of days).
You could also take the difference between two DATETIME YEAR TO MONTH values and get a result that is an INTERVAL YEAR TO MONTH.
However, there is no way to get a difference in years, months and days because there is no simple way to deduce that value. In fact, ISO SQL recognizes two classes of INTERVAL: those in the YEAR-MONTH group, and those in the DAY-SECOND group. You can't have an INTERVAL that crosses the MONTH/DAY barrier.
Use the MDY function :
select mdy(2,15,2014) - mdy(1,15,2014) from sysmaster:sysdual