How to calculate number of days between two fields - one text, one date time RRS feed

  • Question

  • I need to calculate the number of days between 2 fields. Problem is one field is in text (2 digit yr,month,day ). Example August 15 2008 would be 080815 and the other is a Date/Time displayed as 08/21/2008. How do I  write the code to convert one or the other - whichever is best to calculate a number of days between the two? To get an answer of 7 days?


    Wednesday, August 26, 2009 10:00 PM


  • Hi Wad,

    Please try the below query..

    select datediff(day, convert(datetime, '080815'),convert(datetime, '08/21/2008'))

    You can also declare variables and set values and use variables in the query.

    Let me know if you have any more questions.

    Please Vote & "Mark As Answer" if this post is helpful to you.

    Bikash Dash
    • Marked as answer by Zongqing Li Wednesday, September 2, 2009 8:11 AM
    Thursday, August 27, 2009 6:27 AM