I have a table in which date column is stores as varchar.
while doing the Object relational mapping (i am using eclipselink) i want to map this string/varchar to Date object in java.
My table is :
T_BUSINESS TRAN_ID Number, BUSINESS_DATE Varchar
and I want to select all the rows where business date is between two input dates, here problem is since BUSINESS_DATE is varchar i cannot carry out the comparison. I can’t modify the table and i don’t wanna use the Native sql.
Can anybody tell me how can I do this.
Thanks in advance!
You can use a TypeConverter to map this,
See,
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_typeconverter.htm#CHDHCCJF
Doing comparisons in queries may be more difficult, I think the default date format is yyyy-mm-dd so a varchar comparison may work. Otherwise you really need a real DATE field on the database. You could use a database function to convert the varchar to a DATE for comparison, such as using CAST or FUNCTION which EclipseLink support is JPQL.
See,
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/j_cast.htm#cast
http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/j_func.htm#func