Date range comparison in CQ using XPATH - xpath

I'm using the following query to get nodes based on last modified date in CQ.
/jcr:root/content/scaffoldes/properties//*[#jcr:primaryType = 'nt:unstructured' and (#sling:resourceType = 'acme/components/content/scaffoldItem' or #sling:resourceType = 'acme-core/components/data/property') and #jcr:content/cq:lastModified >= xs:dateTime('2000-01-01T00:00:00.000-08:00') and #jcr:content/cq:lastModified < xs:dateTime('2014-12-31T00:00:00.000-08:00') and not(#isHidden)] order by #jcr:score
we have used
http://localhost:4502/crx/explorer/ui/search.jsp
To test this query. But, even after giving a huge date range (2000 - 2016) this query is returning nothing.
But If we remove the date range part, this query returns nodes.
Any pointer to correct this would be helpful.
Thanks and Regards,
San
Notes
The date string is create with the following code:
Calendar cal = Calendar.getInstance();
cal.setTime(start);
String startDate = ValueFactoryImpl.getInstance().createValue(cal).getString();

Sorry for any confusion caused. I found that the node which I'm trying to query doesn't have a cq:lastModified property. So modified the query to include the cq:lastReplicated in the condition.
... and ((#jcr:content/cq:lastModified >= xs:dateTime('2000-01-01T00:00:00.000-08:00') and #jcr:content/cq:lastModified < xs:dateTime('2016-02-11T15:52:57.090-08:00')) or (#jcr:content/cq:lastReplicated >= xs:dateTime('2000-01-01T00:00:00.000-08:00') and #jcr:content/cq:lastReplicated < xs:dateTime('2016-02-11T15:52:57.090-08:00'))) and ...

Related

Get Data of a month from a dated column

Respected all I want to bring data from a dated column with input of a Month like I have a column in database which type is dated but I want data only mentioning just month and year I tried but got errors please check my query
SELECT
M.INV_NUM, M.GD_NUM, M.INV_DATE, M.QTY1,
D.ITEM_CODE, D.HS_CODE, R.QNTY, R.waste_per,
R.WASTE_QNTY, (R.WASTE_QNTY+R.QNTY) TOTAL_CONSUMED
FROM
DOCT_EXPT_SALE_MST M,
DOCT_EXPT_SALE_RAW R,
DOCT_EXPT_SALE_DTL D
WHERE
R.SALE_DET_ID = D.SALE_DET_ID
AND D.SALE_ID = M.SALE_ID
AND M.INV_DATE BETWEEN TO_DATE(TRUNC('072022','MMYYYY')) AND TO_DATE(TRUNC('072022','MMYYYY'))--TO_NUMBER(TO_DATE(TO_CHAR('01072022','DDMMYYYY'))) AND TO_NUMBER(TO_DATE(TO_CHAR('31072022','DDMMYYYY')))
AND M.COMP_CODE = 3;
I tried many things but all is gone in vain. If anybody help me on this, I shall be very thankful my database is 11g
If you are being passed the string '072022' then you can do:
AND M.INV_DATE >= TO_DATE('072022','MMYYYY')
AND M.INV_DATE < ADD_MONTHS(TO_DATE('072022','MMYYYY'), 1)
The TO_DATE('072022','MMYYYY') clause will give you midnight on the first day of that month, so 2022-07-01 00:00:00.
The ADD_MONTHS(TO_DATE('072022','MMYYYY'), 1) clause will take that date and add one month, giving 2022-08-01 00:00:00.
The two comparisons will then find all dates in your column which are greater than or equal to 2022-07-01 00:00:00, and less that 2022-08-01 00:00:00 - which is all possible dates and times during that month.
So your query would be (switching to ANSI joins!):
SELECT
M.INV_NUM, M.GD_NUM, M.INV_DATE, M.QTY1,
D.ITEM_CODE, D.HS_CODE, R.QNTY, R.waste_per,
R.WASTE_QNTY, (R.WASTE_QNTY+R.QNTY) TOTAL_CONSUMED
FROM
DOCT_EXPT_SALE_MST M
JOIN
DOCT_EXPT_SALE_DTL D ON D.SALE_ID = M.SALE_ID
JOIN
DOCT_EXPT_SALE_RAW R ON R.SALE_DET_ID = D.SALE_DET_ID
WHERE
M.INV_DATE >= TO_DATE('072022','MMYYYY')
AND M.INV_DATE < ADD_MONTHS(TO_DATE('072022','MMYYYY'), 1)
AND M.COMP_CODE = 3;

How to check if event's date is within a date range?

I have events (from an Event model) that have a starts_at: value in the form of a datetime. e.g.:
2016-02-18 11:00:00:00000
What I want to be able to do is check whether an event is starting this week.
I want to be able to make a list of events that are occuring this week (starting from the latest Monday).
#events = #calendar.events.where( ... )
I thought something along the lines of this:
start_week = Date.today.beginning_of_week(:monday).day()
end_week = Date.today.beginning_of_week(:monday).day()+6
range = start_week..end_week
#events = #calendar.events.where(starts_at: in range)
But it doesn't take into account the month or year. Also I'm not sure how to write the 'where' clause. How should I go about doing this? Thanks
Try this:
start_week = Date.today.beginning_of_week(:monday)
end_week = Date.today.beginning_of_week(:monday)+6
range = start_week..end_week
#events = #calendar.events.where(starts_at: range)
Assuming you want all the events from the current week, something like this should work:
#events = #calendar.events.where(starts_at: Time.zone.today.all_week)
all_week returns a Date range covering the current week.

Linq DateTime comparison not working

I have the following code:
DateTime timeStamp = Convert.ToDateTime(Request.QueryString["TimeStamp"]);
var result = (from rs in db.VRec
where
rs.TimeStamp == timeStamp &&
rs.Fixure == wFixture
select rs).ToList();
The result shows 0 even though the correct timeStamp is passed.
If I remove the part where I do the TimeStamp comparison:
rs.TimeStamp == timeStamp
The code works fine.
Any idea on why the datetime comparison may not be working?
DateTime has a pretty fine resolution - likely you are comparing timestamps that only differ in milliseconds, which will fail. You probably want something like:
DateTime now = DateTime.Now;
DateTime then = now.Add(TimeSpan.FromMilliseconds(1));
const int EPSILON_MS = 10;
if(now.Subtract(then).TotalMilliseconds < EPSILON_MS)
{
Console.WriteLine("More or less equal!");
}
Linq converts DateTime arguments to DateTime2 in the sql query executed.
That is, when you do the comparison the actual sql executed will compare a DateTime to a DateTime2. This comparison will "cast" the DateTime to a DateTime2 and the millisecond part will be expanded to a greater resolution (in an odd way in my opinion, please enlighten me).
Try to execute the following sql:
declare #d1 datetime = '2016-08-24 06:53:01.383'
declare #d2 datetime2 = '2016-08-24 06:53:01.383'
declare #d3 datetime2 = #d1
select #d1 as 'd1', #d2 'd2', #d3 'converted'
select (case when (#d1 = #d2) then 'True' else 'False' end) as 'Equal',
(case when (#d1 > #d2) then 'True' else 'False' end) as 'd1 greatest'
From the question, I do not know if you want to compare the date with time or only the date part. If you only want to compare date then following would work
var result = (from rs in db.VRec
where
rs.TimeStamp.Date == timeStamp.Date &&
rs.Fixure == wFixture
select rs).ToList();
Since you are using some reference to db, it gives me a feeling that you are fetching your records from database (which ORM you are using is not obvious from the question or tags). Assuming that you are using Entity framework the above query will fail with exception that .Date has no direct translation to sql. If so you can rewrite the query as following to make it work.
var result = (from rs in db.VRec
where
rs.TimeStamp.Day == timeStamp.Day &&
rs.TimeStamp.Month == timeStamp.Month &&
rs.TimeStamp.Year == timeStamp.Year &&
rs.Fixure == wFixture
select rs).ToList();
The benefit of this approach is that you can compare properties to arbitrary deep level i.e you can compare Hours, Minutes,Seconds etc. in your query. The second query is tested in Entity framework 5.

Printing Month Name in Linq

Is there any Inbuilt function in Linq to Print the month Name while working with LINQPAD?
I want to print the month name in the following Scenario
var query = from e in Employees
let month=e.BirthDate.GetValueOrDefault()
let birthmonth=month.ToString("MMMM")
select birthmonth;
query.Dump();
When I run this it is throwing NotSupportedException.
how to print the month name in Linq to Sql?
Rather than using ToString, try string.Format. Something like:
var query = (from e in Employees
let month = e.BirthDate.GetValueOrDefault()
let birthmonth = string.Format("{0:MMMM}", month)
select birthmonth);
query.Dump();
This seems to work from my local testing, although it is not included as part of the SQL query.
Do it in two steps, one to get the months from the database, then another using Linq-To-Objects to perform the formatting.
var birthDates = Employees.Select(e => e.BirthDate).ToList();
var query = birthDates.Select(d => d != null ? d.ToString("MMMM") : "Null");
query.Dump();
Whatever ORM you are using can't convert the string formatting part of you query into SQL that works on your database. So, doing it in two steps and using ToList to evaluate inbetween overcomes that problem.

Count the number of Files modified in the last hour using linq

var query=from file in Directory.GetFiles(source_path, filename, SearchOption.TopDirectoryOnly)
where File.GetLastWriteTime(file) >= DateTime.Now.AddHours(-1)
select file;
Console.Write(query.ToList().Count.ToString());
I'm using the above code to get the count of files modified in last one hour. Looking for a simpler solution. ie to have the count as part of the query itself.
Count() can take a predicate:
Directory.GetFiles(source_path, filename, SearchOption.TopDirectoryOnly).Count(file => File.GetLastWriteTime(file) >= DateTime.Now.AddHours(-1))
You can also call Count() on the query itself.
var source = Directory.GetFiles(source_path, filename,
SearchOption.TopDirectoryOnly);
var timeStamp = DateTime.Now.AddHours(-1);
int count = (from file in source
where File.GetLastWriteTime(file) >= timeStamp
select file)
.Count();
Console.Write(count);
A faster solution would be to use a Count() overload that takes in a predicate.

Resources