Results 1 to 10 of 10

Thread: How to find 'x' number of days from 'yyyymmdd' format?

  1. #1
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80

    How to find 'x' number of days from 'yyyymmdd' format?

    I need to find out the count of number of records older than 100 days from a table having 'order_date' as yyyymmdd format eg. 20041115. Thanks in advance.

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    select datediff(dd,'20031111','20041201')

  3. #3
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    select datediff(dd,'20031111','20041201')
    select datediff(dd,'2003/11/11','2004/12/01')
    select datediff(dd,'2003-11-11 12:00:00','2004-12-01 19:25:00')
    select datediff(dd,'2003-11-11 23:00:00','2004-12-01 19:25:00')

  4. #4
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    I will not be knowing the dates so as to hard code them in the query..instead I need to find out how many records are there which are older than say 100days from a table like this:

    order_number order_date
    1001 20041023
    1002 20041023
    10003 20041023
    7687 20041023
    7909 20041023
    7699 20041023
    9878 20041023

    Any help?

  5. #5
    Join Date
    Feb 2003
    Posts
    1,048
    Select Count(*)
    From myTable
    Where DateDiff(dd, order_date, getdate()) > 100

  6. #6
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    I am getting this error as order_date is declared as int for some reason:

    Server: Msg 8115, Level 16, State 2, Line 1
    Arithmetic overflow error converting expression to data type datetime.

  7. #7
    Join Date
    Feb 2003
    Posts
    1,048
    Set DateFormat ymd

    Select Count(*)
    From myTable
    Where DateDiff(dd, Cast(order_date as varchar)), getdate()) > 100

  8. #8
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    Getting the following error again:

    Server: Msg 174, Level 15, State 1, Line 3
    The datediff function requires 3 arguments.

  9. #9
    Join Date
    Feb 2003
    Posts
    1,048
    Sorry, had a typo in my code:

    Select Count(*)
    From myTable
    Where DateDiff(dd, Cast(order_date as varchar), getdate()) > 100

  10. #10
    Join Date
    Aug 2004
    Location
    USA
    Posts
    80
    It worked! Thank you Rawhide and everyone :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •