Results 1 to 8 of 8

Thread: Arithmetic overflow[resolved]

  1. #1

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358

    Resolved Arithmetic overflow[resolved]

    Here's one for you guys.

    I had this sp.....

    Code:
    CREATE PROCEDURE spVisitors AS
    select convert(varchar,(getdate()-(day(getdate())-1)),101) as startdate, convert(varchar,getdate(),101) as enddate,
    sum([count]) as visits, sum(qcount) as requests, pid
    from tblcounterv3
    where (convert(varchar,cast(countdate as datetime),101) between convert(varchar,(getdate()-(day(getdate())-1)),101) 
    and convert(varchar,getdate(),101))
    group by pid
    GO
    which worked fine until I set the default language settings on my server from English US to English UK.

    Now I get this error :

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


    Any ideas?


    Last edited by thebloke; Sep 23rd, 2005 at 09:38 AM. Reason: resolved
    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Arithmetic overflow

    Try running that SP's code in query analyzer (minus the create procedure part) and print out what values you are getting for the getdates() and the convert(...,101). Perhaps the switch to the UK format is causing problems in the date time formatting.

  3. #3
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Arithmetic overflow

    The only conversion you do to a datetime data type is
    Code:
    cast(countdate as datetime)
    . What format is the countdate column? Try executing this query
    Code:
    select cast(countdate as datetime) from tblcounterv3
    Does it give you the same error?

  4. #4

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358

    Re: Arithmetic overflow

    I did run it in Query Analyser and got the same result. I think the issue is that prior to me changing the settings, the values in the countdate column (which is nvarchar datatype) were being stored as 9/23/2005 however after the change they were going in as 23/09/2005.

    I've tried to run

    Code:
    select cast(countdate as datetime) from tblcounterv3
    and get the same error.....

    however, I've just tried this

    Code:
    'using style 103 instead of 101
    select convert(datetime,countdate,103) from tblcounterv3
    and this returns the date values formatted as 2005-09-23 00:00:00.000

    I tried changing all of the 101's in the original sp to 103's but still get the arithmetic overflow error

    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

  5. #5
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Arithmetic overflow

    You only need to change the convert to datetime. Notce that I have replaced the CAST function with CONVERT.
    Code:
    CREATE PROCEDURE spVisitors AS
    select convert(varchar,(getdate()-(day(getdate())-1)),101) as startdate, convert(varchar,getdate(),101) as enddate,
    sum([count]) as visits, sum(qcount) as requests, pid
    from tblcounterv3
    where (convert(varchar,convert(datetime,countdate,103),101) between convert(varchar,(getdate()-(day(getdate())-1)),101) 
    and convert(varchar,getdate(),101))
    group by pid
    GO
    I really don't understand why you store dates in a varchar column. As you can see it can give you all sorts of problems.

  6. #6

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358

    Resolved Re: Arithmetic overflow

    Sussed it:

    Code:
    select (getdate() - (day(getdate())-1)) as startdate, getdate() as enddate,
    sum([count]) as visits, sum(qcount) as requests, pid
    from tblcounterv3
    where (convert(datetime,countdate,103) between (getdate() - (day(getdate())-1))
    and getdate())
    group by pid
    Thanks guys

    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

  7. #7
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Arithmetic overflow

    Quote Originally Posted by thebloke
    Sussed it:

    Code:
    select (getdate() - (day(getdate())-1)) as startdate, getdate() as enddate,
    sum([count]) as visits, sum(qcount) as requests, pid
    from tblcounterv3
    where (convert(datetime,countdate,103) between (getdate() - (day(getdate())-1))
    and getdate())
    group by pid
    Thanks guys

    So you discovered yourself that you did a lot of uneccessary conversions back and forth between datetime and varchar. Have a nice weekend!

  8. #8

    Thread Starter
    Hyperactive Member thebloke's Avatar
    Join Date
    May 2003
    Posts
    358

    Re: Arithmetic overflow[resolved]

    Yeah, but they weren't unnecessary when I originally wrote the query, just after I changed the settings!

    The Bloke
    www.blokeinthekitchen.com
    making cooking cool for blokes

Posting Permissions

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



Click Here to Expand Forum to Full Width