convert string to date

Last post 11-11-2009 12:04 AM by Sayre. 5 replies.

Sort Posts:

  • convert string to date

    11-03-2009, 5:47 AM

    Hi all,

           I did wrong. In my database table there is a field name RecDate it's datatype is text(MS Access database).

           There are three textboxes first one is for month; it's having 1 to 12 second is for date; its having items 1 to 31 and the third and last one is for year.

    I am saving date like "+DropDownlist1.SelectedItem.Text+"/"+DropDownlist2.SelectedItem.Text+"/"+DropDownlist3.SelectedItem.Text+"/

    It is ok.

    Now when I featching records using query like orderby date. It's not working properly because query reads this date as string. I used this method in many forms. Its very time consuming to change it.

    Is there any other method to do it right.

    help me out pls. what should I do for this condition. I'll never repete this in future. I came to know for date field ur datatype shpould be always datetime.

    Thanks a lot

    never let you down..always be happy...
  • Re: convert string to date

    11-03-2009, 7:18 AM
    Answer
    • Contributor
      4,483 point Contributor
    • fayaz_3e
    • Member since 09-14-2007, 6:15 AM
    • Hyderabad
    • Posts 910

    You can cast it in backend. I mean while retrieving it self.

    Am not sure how to that in MsAccess, but I guess it would be similar to Sql. For sql convert refer this

    http://www.sqlusa.com/bestpractices/datetimeconversion/


    Fayaz
  • Re: convert string to date

    11-03-2009, 7:54 AM
    Answer
    • Star
      9,340 point Star
    • hans_v
    • Member since 01-29-2007, 9:03 PM
    • Posts 1,617

    demoninside9:
    Is there any other method to do it right.
     

    Start by changing the recDate field to a DateTime, not a text field.

    instead of 3 dropdowns, why not use only 1 textbox, an use for example the Ajax Control Toolkit Calender extender:

    http://www.asp.net/AJAX/AjaxControlToolkit/Samples/Calendar/Calendar.aspx

    and about using dates with Access, read this:

    http://www.mikesdotnetting.com/Article/92/MS-Access-Date-and-Time-with-ASP.NET

  • Re: convert string to date

    11-03-2009, 7:55 AM
    Answer
    • Participant
      813 point Participant
    • johnyM456
    • Member since 09-28-2009, 1:28 PM
    • Belgium
    • Posts 141

    Yep, always use the DateTime format. It's made for this reason.

    For now you could solve your problem by using this in your query's:

     

    "SELECT *, DateSerial ([YearField]; [MonthField]; [DayField]) AS MyDate FROM MyTable WHERE ...blablabla..."

     

    Good luck,

  • Re: convert string to date

    11-03-2009, 8:57 AM
    Answer
    • Participant
      914 point Participant
    • Babunareshnarra
    • Member since 07-12-2009, 7:10 AM
    • Hyderabad
    • Posts 183

    use this,

    declare @dtchar nvarchar(20)
    set @dtchar='11/29/2009'        //11 - Month,29 - Date and year
    select convert(datetime,@dtchar)

    O/P is  2009-11-29 00:00:00.000



    Regards

    Babu Naresh Narra

    Remember to click “Mark as Answer” on the post If you get answer from my post(s) !
  • Re: convert string to date

    11-11-2009, 12:04 AM
    • Participant
      1,398 point Participant
    • Sayre
    • Member since 07-24-2008, 5:03 AM
    • Manila, Philippines
    • Posts 349

    Hi demoninside9

    Another info about the date and time

    http://codeincsharp.blogspot.com/2008/05/different-date-format-in-c.html 

    Hope This One Can Help.
    Thanks

    To Be Happy Is To Be YourSelf

    codeincsharp.blogspot.com
Page 1 of 1 (6 items)