January 13, 2010 at 11:21 am
Hi Gurus,
I'm trying to execute the following sql-statement which should show the date entry of the weekdays from monday-sunday on ms sql 2000:
SELECT DATEADD(dd,-(DATEPART(dw, Datum) - 1),datum) FROM "pzw"."stunden_verwaltung"
UNION ALL
SELECT DATEADD(dd,-(DATEPART(dw, Datum) - 2),datum) FROM "pzw"."stunden_verwaltung"
UNION ALL
SELECT DATEADD(dd,-(DATEPART(dw, Datum) - 3),datum) FROM "pzw"."stunden_verwaltung"
UNION ALL
SELECT DATEADD(dd,-(DATEPART(dw, Datum) - 4),datum) FROM "pzw"."stunden_verwaltung"
UNION ALL
SELECT DATEADD(dd,-(DATEPART(dw, Datum) - 5),datum) FROM "pzw"."stunden_verwaltung"
UNION ALL
SELECT DATEADD(dd,-(DATEPART(dw, Datum) - 6),datum) FROM "pzw"."stunden_verwaltung"
UNION ALL
SELECT DATEADD(dd,-(DATEPART(dw, Datum) - 7),datum) FROM "pzw"."stunden_verwaltung"
The statement runs at the beginning but after a while i'm getting an overflow error, code 517.
Could somebody help me?. Is there any way to rewrite this code that I'll not get a overflow error?
Thanks
Mamadou
January 13, 2010 at 11:58 am
Is Datum a date field? What are the min/max values in the field?
This might take some work, but here's how you might narrow down which record is causing the issue - run the queries one at a time (without the UNION) until you find one that thows the error. Add a where clause on the Datum field for that query and restrict it more and more until you determine the datum value that causes the error. At that point, you might then know what the issue is and how to fix it, or you can post back here with the datum value that isn't working and maybe I (or the many people who come here much smarter than I) will have an idea how to fix it.
Chad
January 13, 2010 at 2:02 pm
Hallo, it is a datetime field.
January 13, 2010 at 2:14 pm
what are the min and max values stored in the column?
Is there anything else done outside the code you posted that might be causing the error (if you run the exact code you posted, do you get the error)? I tried several values, but couldn't get the error (I'm using 2K5 though, so there is some difference there).
Chad
January 14, 2010 at 2:04 am
Hi,
thanks for your repply.
min value = 0005-11-11
max value = 2010-01-29
Could the min value be the problem?
Thanks
Mamadou
January 14, 2010 at 8:22 am
Yes, that would be the problem. I'm not sure how you got that value into a datetime field though. When I run this script:
DECLARE @Datum Datetime
SELECT @Datum = '0005-11-11'
SELECT DATEADD(dd,-(DATEPART(dw, @Datum) - 6),@Datum)
I get an error when trying to cast '0005-11-11' as a datetime, so it doesn't even get to the dateadd/datepart. I'm pretty sure that date is causing your issue though - is the proper year for this date really the year "0005", or is that some bad/corrupted data?
I think the error 517 is saying that when the DATEADD executes, it is adding a value that takes the date out of the range of appropriate values. The datetime field ranges from 1/1/1753 to 12/12/9999 so DATEADD(dd, 1, '12/31/9999') yields this error as does DATEADD(dd, -1, '1/1/1753'), since both of those push the result out of the range of valid values. That being the case, DATEADD(dd, 1, '0005-11-11') would also give you this error since it is outside the range of a datetime, but you should be getting error 242 instead since 11/11/0005 is outside the range of valid dates to begin with.
Chad
January 14, 2010 at 1:41 pm
Hallo Chad, I've also got the error code 242 you mentioned in your last email. Is there a way to repare these entrys?
Thanks
Mamadou
January 14, 2010 at 2:32 pm
Are you really really sure Datum is a datetime field and not a char or varchar field?
Any chance you are using SQL Server 2008 and not SQL2000 or SQL7 and it is a datetime2 field?
I understand the errors, but they don't make sense for a datetime field. If you are using 2008, or the data is in a char or varchar (or nchar or nvarchar) field then the errors make more sense.
Anyway - what you need to do is find the records that are bad, and either set them to NULL or set them to a valid value using an UPDATE statement. You'll have to decide what is the most appropriate new value for your data, I won't be much help there. Alternatively, you could try to use something to remove those rows from the select query. Something like ISDATE or using greater-than and less-than in the WHERE clause might work. If you want me to work up something, I'd be happy to, but I'd need the table structure and some sample data in a format similar to the one described here: How to Post Data and Code[/url].
Chad
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply