db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stephan van Loendersloot (LIST)" <step...@republika.nl>
Subject Re: Derby/Oracle - single SQL query?
Date Wed, 04 Feb 2009 18:34:32 GMT

Kathey Marsden wrote:
> Kent Spaulding wrote:
>>
>> Is there some query format that will for both?
> I don't have access to Oracle, but would it work to use an explicit cast
> and cast(d.insertion_date as date) <= CAST('2009-02-28'  AS DATE);
>
>
I don't have access to Oracle either, so my preffered solution would be 
(again) to use a PreparedStatement

Example code (not very efficient, the DateFormat should be reused 
instead of recreating it each time. Preferrably by using it from a 
ThreadLocal since it's not thread-safe):

public static void getSqlDate(String date) {
    java.sql.Date result = null;
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    try {
       result = new java.sql.Date(dateFormat.parse(date).getTime());
    } catch (ParseException e) {
       // Logging goes here
    }
    return result;
}


String strDate = "2009-02-28";

PreparedStatement pstmt = conn.prepareStatement("SELECT d.insertion_date 
FROM d WHERE CAST(d.insertion_date as date) >= ?");
pstmt.setDate(1, getSqlDate(strDate));


Regards,

    Stephan.




Mime
View raw message