SQL DateAdd

So, I'm using DateAdd.  It is storing data in an internal table via Select .... Into #table From table.  In the internal table I am calculating date1 field with DateAdd.

I am then appending results of #table to another table #result (which was created using Create Table).  In the create table I have two fields date1 and date2 which are both declared as "smalldatetime" fields.  

I am receiving an error "Adding a value to a 'datetime' column caused an overflow.  The statement has been terminated".  

Seems that the issue is that the DateAdd returns a DATETIME so when I put the values in my date1 field I receive the overflow error.

How do I cast my formula so it returns as a SMALLDATETIME?

DATEADD(day,1,startDT) as date1,

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David ToddSenior Database AdministratorCommented:

What is the value so produced? That is, you have a startDT around 5th June 2079, and that I think is causing your overflow.

I can show you how to do the explicit cast, but that wont solve your overflow - which begs the question: Do you need a smalldatetime, or is it time to move to a datetime?

This code illustrates your problem, and that the casts don't fix it.

-- This works
declare @StartDT as smalldatetime

set @StartDT = getdate()

	dateadd( day, 1, @StartDT )
	, convert( smalldatetime, dateadd( day, 1, @StartDT ))
	, cast( dateadd( day, 1, @StartDT ) as smalldatetime )


-- this gives overflow
declare @StartDT as smalldatetime

set @StartDT = '2079-06-06 15:35'

	--dateadd( day, 1, @StartDT )
	convert( smalldatetime, dateadd( day, 1, @StartDT ))
	, cast( dateadd( day, 1, @StartDT ) as smalldatetime )


-- select to find row that is causing issue
select st.*
from dbo.SomeTable st
    st.startDT > '2079-06-06'

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulEE Topic AdvisorCommented:
I agree with David,
sounds like after the dateadd the value generated is larger than the maximum permitted for that column's data type

Date range
1900-01-01 through 2079-06-06
Vitor MontalvãoMSSQL Senior EngineerCommented:
CipherIS, do you still need help on this question?
CipherISAuthor Commented:
Sorry for the delay.  Working on another production issue.  I implemented your solution and it resolved this production issue.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.