How to Convert string into date

Posted on 2006-05-01
Last Modified: 2008-02-20
hello i am using 2 textbox like t1 and t2

in t1 i have 31/03/2006 and in t2 i have 30/04/2006

i want to insert these values in date column in the tables.

table test
 t1 date
t2 date

Please suggest me and help me
Thanks in Advance.

Question by:rehman123
    LVL 3

    Expert Comment


    Please find the below code.

    SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy");
    Date dt = sdf.parse(tr.value);

    Albin Joseph
    *advertising removed by Netminder 31 Aug 2006*
    LVL 35

    Expert Comment

    what language and what database are you using?

    If Access, then when building your sql query, put # signs arounf the date field. And if SQL Server, then put sinle quotes:

    for Access:

    sql="INSERT INTO test(t1,t2) VALUES (#" & t1.Text & "#,#" & t2.Text & "#)"

    for SQL Server:

    sql="INSERT INTO test(t1,t2) VALUES ('" & t1.Text & "','" & t2.Text & "')"


    Author Comment


    I am using and Oracle as a Database .
    Same way i am trying but it is giving error that not a date format .

    [InvalidCastException: Cast from string "31/03/2006" to type 'Date' is not valid.]

    please suggest
    LVL 4

    Expert Comment

    Try changing the date format, I know i get this eror regularly in MS SQL when trying to use xx/xx/xxxx
    Instead I have to format the date like SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
    Give that a shot, not sure if its the same for oracle or not.
    LVL 35

    Accepted Solution

    sql="INSERT INTO test(t1,t2) VALUES ('" & CDate(t1.Text) & "','" & CDate(t2.Text) & "')"

    Author Comment

    Session.LCID = 2057

    type this code in the page_load event
    and use cdate function it will work
    mclSql3 = "insert into fs_separation_details ( fsd_id, fsd_emp_id, fsd_issued_date,fsd_ic_id,fsd_bonus_amount"
            mclSql3 = mclSql3 & " ,fsd_hod_discussion,FSD_LAST_WORKING_DATE)"
            mclSql3 = mclSql3 & " values(seq_fsd.NEXTVAL, '" & lblempid.Text & "',sysdate,'" & cmd_sep_reason.SelectedValue & "','" & LblBonus.Text & "'"
           mclSql3 = mclSql3 & ",'" & txthod.Text & "', to_date(‘" & CDate(txtLwd.Text) & "’,’dd/mm/yyyy’) )"

    Any way thanks a lot .



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    When pages do not download correctly, and you don't know why, the first thing you do is to look at the HTML source code of that page, but not all the downloaded files appear always clearly. If your source includes a javascript that computes the name…
    Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now