RE: DateTime format during INSERT using MS JDBC driver

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Yilei Wang (yileiw_at_online.microsoft.com)
Date: 08/17/04


Date: Tue, 17 Aug 2004 19:06:20 GMT


 Hi Tim,

You can either use the parameterized query and pass in a value of a
Java.Sql.Date type. You can thus convert the "yyyy-mm-dd" string to Date.
Below is the code snippet:
                PreparedStatement st = connection1.prepareStatement("INSERT
Customers (ArchiveDate) VALUES (?)");
        st.setDate(1, Date.valueOf("1999-01-30"));
        st.executeUpdate();

Or if you choose to hard code the "yyyy-mm-dd" in the query string, you can
use CONVERT function to convert the string to a datetime sql data type
according to the proper style of date format. 120 is the ODBC canonical
style that converts yyyy-mm-dd hh:mi:ss(24h). Below is the code snippet:
               PreparedStatement st = connection1.prepareStatement("INSERT
Customers (ArchiveDate) VALUES (CONVERT (datetime, '1999-01-30', 120) )");
               st.executeUpdate();

For more info on CONVERT, please refer to the SQL Server Books Online.

Yilei

        



Relevant Pages

  • Re: Terminolgy: the verb corresponding to "toString"
    ... /** returns date converted to an ISO date String of the form ... YYYY-MM-DD in local time. ... > @param theDate date you want to display. ...
    (comp.lang.java.programmer)
  • Re: ASP > SQL date problem!?
    ... the general accepted format is yyyy-mm-dd ... I do not use SQL server. ... But if you 'give' it to the db as a ASP string, ...
    (microsoft.public.inetserver.asp.general)
  • Re: ASP > SQL date problem!?
    ... the general accepted format is yyyy-mm-dd ... I do not use SQL server. ... But if you 'give' it to the db as a ASP string, ...
    (microsoft.public.access.adp.sqlserver)
  • Java Date Formatting
    ... I have a date in string format 'yyyy-mm-dd' ... I want to reformat this to 'dd-MMM-2004' so I can use it in an insert ...
    (comp.lang.java.programmer)