how to add day to date in ejbql - ejb

I am trying to achieve dateadd in my EJB query. I tried something like this but it doesn't work:
select t.date + 1 from Table t
Once I tried executing that code, this exception comes out:
org.hibernate.exception.SQLGrammarException: ERROR: operator does not exist: timestamp without time zone + integer Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 93
I also tried casting 1 to date and interval but the query result would only be null.
I just wonder, is it really possible to add days in a given date in an EJB query

There is no support for date arithmetic and also no possibility to extract day from date in EJBQL, and also not in JPQL.
Depending about JPA provider (Hibernate, EclipseLink etc.) there can be useful vendor specific extensions and possibility to call database functions. For example with EclipseLink FUNC can be used.

Related

Compare Two date "Strings" Sqlite using Nhibernate

I am using SQLite and NHibernate and Im storing my date as Strings on SQLite since I cant store as Date.
Everything was just fine until a need to compare dates.. I tried the following codes:
var initialDate = DateTime.Parse(_InitialDate);
var finalDate = DateTime.Parse(_FinalDate);
return session.QueryOver<Locacoes>()
.Where(c => DateTime.Parse(c.InitialDate) >= initialDate )
.Where(c => DateTime.Parse(c.FinalDate) <= finalDate).List();
but I got an exception on the first "Where": "a variable 'c' of type 'Locacoes' is referenced on scope '', but it is not defined".
How can I compare date on SQLite using NHibernate?
I've tried many things with above code, but didnt work.
Edit: Its not duplicate, the error can be the same, but the result is different
First, while Sqlite likes to pretend everything is a string, that should be considered a storage format and NO REASON why your object model should use strings to hold date values. Your object model should of course type the properties as DateTime or DateTimeOffset. Then configure your NHibernate mappings properly to map the values to what SQLite can handle (actually I think NHibernate would handle that automatically if you just tell it to use the SQLiteDialect).
Second, I don't think QueryOver() can handle things like DateTime.Parse(). Don't confuse QueryOver() with Linq2NHibernate (the Query() method), which have more advanced expression interpretation abilities. On the other hand, you no longer need to use Parse() when you begin to use correct types in your object model.
At least if you use LINQ, it should be able to handle DateTime.Date for SQLite, if you need it:
session.Query<Locacoes>()
.Where(l => c.InitialDate.Date >= initialDate)
Of course, you would only need to put Date in there if there is a non-zero time-of-day component that you need to ignore.

Using DATEPART in sql statement to get records with specific year

In Classic ASP:
I can extract the year from a date/time field:
tester=rs.fields("datestamp")
tester=DATEPART("yyyy",tester)
But I cannot seem to figure out how to make this work in a SQL statement to bring all the records from a specific year:
Select * from table1 where DATEPART("yyyy",datestamp)='2012'
and this doesn't work either:
Select * from table1 where DATEPART("yyyy",datestamp)=2012
I've looked through a zillion examples, here and elsewhere, and can't seem to find one that'll make this work. What am I doing wrong?
The function DatePart can extract from any date some values.
The best explanation that i know is here: W3School.com
And this command can be used as a part of SQL string as you want, but in this case you must considerer that the main parameter change.
Sample for filter by Month for less that June:
DATEPART(month, yourvar_withdate) <= 6
Check this explanation: W3School.com-SQL
Sure that you need use a number without quotes to eval. You can check "yy" or year (without quotes) to verify.
One more note, you must have always content on DateStamp field or receive an error.

Extract day of week from database field in PostgreSQL

I simply want to show a date and its day of the week from a table.
The following works:
select "invDate", (select extract (dow from timestamp '2014-09-22'))
from "tblInvMaster"
But the moment I try to use the actual field like the example below, it doesn't work:
select "invDate", (select extract (dow from timestamp "invDate"))
from "tblInvMaster"
The above gives a syntax error where the field name starts in timestamp.
What is the correct method of getting this to work?
The syntax
TYPENAME 'VALUE'
e.g.
TIMESTAMP '2014-01-01'
is only valid in SQL for type literals.
If you want to cast a non-literal value you must use an explicit cast. Most likely you don't require a cast at all, and can just write:
extract(dow from "invDate")
as "invDate" should already be a timestamp or date. If it isn't, you'll need to CAST("invDate" AS timestamp).

F# query expressions - restriction using string comparison in SqlProvider with SQLite

SQLite doesn't really have date columns. You can store your dates as ISO-8601 strings, or as the integer number of seconds since the epoch, or as Julian day numbers. In the table I'm using, I want my dates to be human-readable, so I've chosen to use ISO-8601 strings.
Suppose I want to query all the records with dates after today. The ISO-8601 strings will sort properly, so I should be able to use string comparison with the ISO-8601 string for today's date.
However, I see no way to do the comparison using the F# SqlProvider type provider. I'm hoping that this is just a reflection of my lack of knowledge of F# query expressions.
For instance, I can't do:
query {
for calendarEntry in dataContext.``[main].[calendar_entries]`` do
where (calendarEntry.date >= System.DateTime.Today.ToString("yyyy-MM-dd hh:mm:ss"))
... }
I get:
The binary operator GreaterThanOrEqual is not defined for the types 'System.String' and 'System.String'.
I also can't do any variation of:
query {
for calendarEntry in dataContext.``[main].[calendar_entries]`` do
where (calendarEntry.date.CompareTo(System.DateTime.Today.ToString("yyyy-MM-dd hh:mm:ss")) >= 0)
... }
I get:
Unsupported expression. Ensure all server-side objects appear on the left hand side of predicates. The In and Not In operators only support the inline array syntax.
Anyone know how I might do string comparisons in the where clause? It seems that my only option for filtering inside the query is to store seconds-since-epoch in the database and use integer comparisons.
This was a temporary bug with old SQLProvider version and it should be working now. If not, please open a new issue to the GitHub repository: https://github.com/fsprojects/SQLProvider

Retrieving Data From Returned Oracle Timestamp Column

We have a ColdFusion 8 (Linux) application that uses an Oracle timestamp. We just converted to Oracle 11g from 10g and we're now using Oracle's thin client on the data sources. We're getting an error in the application where a timestamp column is selected.
It seems as though an object of class oracle.sql.TIMESTAMP is being returned. I verified this by dumping the contents of the column. Sure enough, it gives me a break down of the object's methods and their return types. But, I can't seem to be able to interface with this object directly:
<cfquery name="getstuff" ...>
SELECT timestampfld ...
FROM myTable
</cfquery>
getstuff.timestampfld contains an object. But, doing this:
<cfoutput query="getstuff">
#timestampfld.stringValue()#
#timestampfld.dateValue()#
</cfoutput>
produces the error that says those methods can't be found. How can I get at the data held in that object?
Update from comments:
When I take column value and apply the DateFormat( timestampfld, "dd.mm.yyyy" ) function to it. The CF error is
"The value class oracle.sql.timestamp cannot be converted to a date".
When I perform <cfoutput>, I get the class definition.
In the retrieved column, I seem to be getting an object instead of a string. When I cfdump the column, I get OBJECT OF oracle.sql.TIMESTAMP. The dump lays out the methods and fields available. When I cfoutput that same variable, a string is displayed. When I try to perform a DataFormat() on the variable, it complains that its not a date.
I just happened to stumble over this error during my development. I had it in the past and long forgotten since.
I know of two ways to mitigate:
The first is according to an answer of a question regarding the same error message in a different context.
One would add the following to the jvm.config file
-Doracle.jdbc.J2EE13Compliant=true
The second is not to return an Oracle TIMESTAMP column but CAST it do DATE, first, like
<cfquery name="getstuff" ...>
SELECT CAST( timestampfld as DATE ) timestampfld
FROM myTable
</cfquery>
I'm not satisfied with either, to be honest. When the JVM argument is forgotten, the software crashes. The CAST, on the other hand, may influence how the SQL works.

Resources