locked
Get the first x characters of a string RRS feed

  • Question

  • Hi All

    I am getting a date from a database in the format: 2010-11-23 21:38:07.000

    However I just want to use the date part and not the time (e.g. 2010-11-23), what code can i use to trim the string down to just the first 10 characters?

     

    Thanks

    Sunday, February 20, 2011 3:43 PM

Answers

  • Hi All

    I am getting a date from a database in the format: 2010-11-23 21:38:07.000

    However I just want to use the date part and not the time (e.g. 2010-11-23), what code can i use to trim the string down to just the first 10 characters?

     

    Thanks

    string date=datefrom_db;
    date=date.Substring(x);//x is char num from begening on 0 basis;
    
    


    Regards Kumar Gaurav.
    • Marked as answer by geraintf Sunday, February 20, 2011 4:34 PM
    Sunday, February 20, 2011 4:09 PM
  • hi,

    I am getting a date from a database in the format: 2010-11-23 21:38:07.000

    The question is: do you get this value as string or as date/time value. Don't mixup the representation of a DateTime value and the value itself.

    When you get a date/time value, thus the type in C# is DateTime, then you can simply use the Date property:

    http://msdn.microsoft.com/en-us/library/system.datetime%28v=VS.100%29.aspx

    When you get it as a String, then you can use the Left() function:

    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.left%28v=VS.100%29.aspx

    btw, when you get it as a String, then you should modify your data retrieval to get it as DateTime as it's better to get it as native type.


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    • Marked as answer by geraintf Sunday, February 20, 2011 4:34 PM
    Sunday, February 20, 2011 3:54 PM
  • Thanks for both your replies,

     

    I solved the problem by changing my SQL:

     

    select convert(varchar,DateColumn,101)

    • Marked as answer by geraintf Sunday, February 20, 2011 4:34 PM
    Sunday, February 20, 2011 4:34 PM

All replies

  • hi,

    I am getting a date from a database in the format: 2010-11-23 21:38:07.000

    The question is: do you get this value as string or as date/time value. Don't mixup the representation of a DateTime value and the value itself.

    When you get a date/time value, thus the type in C# is DateTime, then you can simply use the Date property:

    http://msdn.microsoft.com/en-us/library/system.datetime%28v=VS.100%29.aspx

    When you get it as a String, then you can use the Left() function:

    http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.left%28v=VS.100%29.aspx

    btw, when you get it as a String, then you should modify your data retrieval to get it as DateTime as it's better to get it as native type.


    Microsoft MVP Office Access
    https://mvp.support.microsoft.com/profile/Stefan.Hoffmann
    • Marked as answer by geraintf Sunday, February 20, 2011 4:34 PM
    Sunday, February 20, 2011 3:54 PM
  • Hi All

    I am getting a date from a database in the format: 2010-11-23 21:38:07.000

    However I just want to use the date part and not the time (e.g. 2010-11-23), what code can i use to trim the string down to just the first 10 characters?

     

    Thanks

    string date=datefrom_db;
    date=date.Substring(x);//x is char num from begening on 0 basis;
    
    


    Regards Kumar Gaurav.
    • Marked as answer by geraintf Sunday, February 20, 2011 4:34 PM
    Sunday, February 20, 2011 4:09 PM
  • Format it from the database instead of in C#?

     

    SELECT CONVERT(VARCHAR(10),YOURDATECOLUMNHERE,121) AS DATE FROM YOURTABLE HERE

     

    That's if you want it in the format you requested.

    Sunday, February 20, 2011 4:17 PM
  • Thanks for both your replies,

     

    I solved the problem by changing my SQL:

     

    select convert(varchar,DateColumn,101)

    • Marked as answer by geraintf Sunday, February 20, 2011 4:34 PM
    Sunday, February 20, 2011 4:34 PM
  • yourDate.Date;
    Thursday, December 21, 2017 6:44 AM