Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Why is this insert Failing.

Author  Topic 

Dallr
Yak Posting Veteran

87 Posts

Posted - 2008-02-14 : 17:17:27
Good Afternoon All,
I have the following code where if i use only one insert statement it works. But when I try to do more than one it fails.
I am not understanding why.

Use TEST
GO
CREATE TABLE YourTable
(PersonID INT
, TreatmentID INT
, StartDate DATETIME
, EndDate DATETIME
, TreatmentType INT);
GO
--PersonID, TreatmentID, StartDate, EndDate, TreatmentType
DElETE FROM YourTable;
INSERT INTO YourTable VAlues (1,1,'01/01/2008','01/02/2008',3)
go
INSERT INTO YourTable Values( 1,2,'22/01/2008','28/02/2008',1)
go
INSERT INTO YourTable Values( 1,3,'01/04/2008','31/12/2008',2)
go
INSERT INTO YourTable Values( 2,4,'01/01/2008','31/12/2008',1)
go
INSERT INTO YourTable Values( 2,5,'23/06/2008','31/12/2008',3)
go
INSERT INTO YourTable Values( 3,6,'01/01/2008','28/02/2008',1)
go
INSERT INTO YourTable Values( 3,7,'05/11/2008','23/12/2008',2)
go



The error specifics are:
quote:
Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.


Thanks in advanced for any assitance.

Dane

jhocutt
Constraint Violating Yak Guru

385 Posts

Posted - 2008-02-14 : 17:28:54
try

SET DATEFORMAT dmy

before your inserts


"God does not play dice" -- Albert Einstein
"Not only does God play dice, but he sometimes throws them where they cannot be seen."
-- Stephen Hawking
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-02-14 : 17:43:44
Also, when entering dates in this fashion, make use of ISO format - yyyymmdd to avoid such kind of problems.

INSERT INTO YourTable Values( 1,2,'20080122','20080228',1)


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

Dallr
Yak Posting Veteran

87 Posts

Posted - 2008-02-14 : 17:44:22
Thanks That worked nicely. But I have another question. Why is it that i had to use that? To me it is a simple insert that I was trying to do. Did it have someting to do with the collation setting on my computer?

DM
Go to Top of Page

Dallr
Yak Posting Veteran

87 Posts

Posted - 2008-02-14 : 17:54:03
James and Harsh Thanks for the assistance .

Dane
Go to Top of Page
   

- Advertisement -