Tuesday, June 23, 2009

sql to_date() to_char() removal for java

Oracle to_date()
select db_fieldname from db_table where db_timestamp_field = to_date(?, 'dd-mm-yyyy');

replace the to_date(?, 'dd-mm-yyyy') function with a ?

select db_fieldname from db_table where db_timestamp_field = ?;

then using

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Timestamp result = new Timestamp(sdf.parse(inputdate,new ParsePosition(0)).getTime());

prepareStatement.setTimeStamp(1,result);


Oracle to_char()

select * from table where to_char(db_timestamp_field, 'dd-mm-yyyy') = ?;

replace to_char(db_timestamp_field,'dd-mm-yyyy') with trunc(db_timestamp_field)

select * from table where trunc(db_timestamp_field) = ?;

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date javaDate = sdf.parse(inputdate);
java.sql.Date sqlDate = new java.sql.Date(javaDate.getTime());

prepareStatement.setDate(1,sqlDate);

No comments:

Post a Comment