2

我很难使用 SQL2 查询语言来比较日期。

我希望能够做这样的事情:

SELECT p.* FROM [nt:base] AS p WHERE EXTRACT(YEAR FROM p.[some_date]) = "2008"

可能吗?这些约束之王的最佳技术是什么?

谢谢你。

4

1 回答 1

6

The best way to use dates in a query is to use CAST to force the conversion of a string representation of a date/time into a DATE value used by JCR.

Unfortunately, in your example you're looking for a date that occurs within a particular year. There's no way in JCR to extract the year as an integer, so the only standard way I know of is to check whether the actual date occurs within a range defined by the first the first and last microsecond of the year:

SELECT p.* FROM [nt:base] AS p
WHERE p.[some_date] >= CAST('2008-01-01T00:00:00.000Z' AS DATE)
  AND p.[some_date] <= CAST('2008-12-31T23:59:59.999Z' AS DATE)

You can of course use a long representation (the number of milliseconds past epoch) rather than the string literal.

This becomes much easier if you have some kind of 'year' property:

SELECT p.* FROM [nt:base] AS p
WHERE p.[some_year] = 2008

You could also use a string literal and force a cast to LONG:

SELECT p.* FROM [nt:base] AS p
WHERE p.[some_year] = CAST('2008' AS LONG)

If the some_year property is defined on a property definition to be LONG, then some implementations may discover this and auto-convert during planning.

于 2013-02-27T17:11:25.630 回答