473,545 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Out of range datetime value error when inserting using select...union

Hi all,
I am getting this error when insert values from one table to another in
the first table the values are varchar (10). In the second they are
datetime. The format of the data is mm/dd/yyyy to be easily converted
to dates. The conversion in this case is implicit as indicated in SQL
Server documentation. Here is my query:

INSERT INTO Campaign (CampaignID, Name, DateStart, DateEnd, ParentID,
ListID)
SELECT mysqlfactiva.db o.campaigns.cam paign_id AS CampaignID,
mysqlfactiva.db o.campaigns.cam paign_name AS Name,
MIN(mysqlfactiv a.dbo.programs. start_date) AS DateStart,

MIN(mysqlfactiv a.dbo.programs. end_date) AS DateEnd,
NULL AS ParentID,
NULL AS ListID
FROM mysqlfactiva.db o.campaigns, mysqlfactiva.db o.programs
WHERE mysqlfactiva.db o.campaigns.cam paign_id =
mysqlfactiva.db o.programs.camp aign_id
GROUP BY mysqlfactiva.db o.campaigns.cam paign_id,
mysqlfactiva.db o.campaigns.cam paign_name,
mysqlfactiva.db o.campaigns.des cription
UNION
SELECT program_id + 100000, program_name, start_date, end_date,
campaign_id AS ParentID, NULL AS ListID
FROM mysqlfactiva.db o.programs
UNION
SELECT execution_id + 200000, execution_name, start_date,
end_date, program_id + 100000 AS ParentID, NULL AS ListID
FROM mysqlfactiva.db o.executions
UNION
SELECT wave_id + 300000, wave_name, start_date, end_date,
mysqlfactiva.db o.waves.executi on_id + 200000 AS ParentID, NULL AS
ListID
FROM mysqlfactiva.db o.waves, mysqlfactiva.db o.executions
WHERE mysqlfactiva.db o.waves.executi on_id =
mysqlfactiva.db o.executions.ex ecution_id

I am referencing programs table two times. If I just select this all I
get all data I need. When doing insert I get a message:

Server: 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.

If I execute just first part of the query before first union, I insert
data fine:

INSERT INTO Campaign (CampaignID, Name, DateStart, DateEnd, ParentID,
ListID)
SELECT mysqlfactiva.db o.campaigns.cam paign_id AS CampaignID,
mysqlfactiva.db o.campaigns.cam paign_name AS Name,
MIN(mysqlfactiv a.dbo.programs. start_date) AS DateStart,

MIN(mysqlfactiv a.dbo.programs. end_date) AS DateEnd,
NULL AS ParentID,
NULL AS ListID
FROM mysqlfactiva.db o.campaigns, mysqlfactiva.db o.programs
WHERE mysqlfactiva.db o.campaigns.cam paign_id =
mysqlfactiva.db o.programs.camp aign_id
GROUP BY mysqlfactiva.db o.campaigns.cam paign_id,
mysqlfactiva.db o.campaigns.cam paign_name,
mysqlfactiva.db o.campaigns.des cription
As soon as I use union I get the above error. This is very strange
since even when I execute the query using first union where the dates
come from the same table 'programs' I get the error. Why I can insert
from programs first time and can's second time?

Any help will be appreciated.

Thanks,
Stan

Mar 6 '06 #1
1 5462
(su*********@ya hoo.com) writes:
I am getting this error when insert values from one table to another in
the first table the values are varchar (10). In the second they are
datetime. The format of the data is mm/dd/yyyy to be easily converted
to dates. The conversion in this case is implicit as indicated in SQL
Server documentation. Here is my query:
Note that the interpretation is dependent on the settings for DATEFORMAT
and LANGUAGE.
INSERT INTO Campaign (CampaignID, Name, DateStart, DateEnd, ParentID,
ListID)
SELECT mysqlfactiva.db o.campaigns.cam paign_id AS CampaignID,
mysqlfactiva.db o.campaigns.cam paign_name AS Name,
MIN(mysqlfactiv a.dbo.programs. start_date) AS DateStart,

MIN(mysqlfactiv a.dbo.programs. end_date) AS DateEnd,
NULL AS ParentID,
NULL AS ListID
FROM mysqlfactiva.db o.campaigns, mysqlfactiva.db o.programs
So the data type of mysqlfactiva.db o.programs.star t_date is varchar(10)?

Excuse me, if I am nosy, but a MIN on that columns appears somewhat
funny to me. If there is a date in early January you will use that
date, no matter which year it is in?

Better would be

MIN (CASE WHEN isdate(start_da te) = 1 THEN start_date END
As soon as I use union I get the above error. This is very strange
since even when I execute the query using first union where the dates
come from the same table 'programs' I get the error. Why I can insert
from programs first time and can's second time?


In the first query, you are only taking the first value, in the
second query you are taking all the values. This query should give
you the bad data:

SELECT * FROM mysqlfactiva.db o.programs.star t_date
WHERE isdate(start_da te) = 0 OR isdate(end_date ) = 0

By the way, I seem recall that MySQL has a very liberal view on what
is a good date...
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Mar 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
58164
by: Dave Pylatuk | last post by:
Hello all. I am attempting to insert a row into a table with a datetime column: When the insert statement contains a value for the millisecond portion of the data time column: ie. {ts '2003-11-05 12:02:43:2960'} I get 'Syntax error converting datetime from string' When I insert a value like: {ts '2003-11-05 12:02:43'}
5
7486
by: ratu | last post by:
I'd like to use a stored procedure to insert large amounts of records into a table. My field A should be filled with a given range of numbers. I do the following ... but I'm sure there is a better (faster) way: select @start = max(A) from tbl where B = 'test1' and C = 'test2' while @start <= 500000 begin insert into tbl (A, B, C) values...
2
8127
by: ralamo | last post by:
When i execute the following insert query, the above mentioned error is coming. Anything wrong in the query? INSERT INTO ECN_1 (Old_SbPartNo, Old_PartDesc, Old_ManPartNo, Old_Manuf, Old_Vendor, Old_RefDesi, Old_Qty, New_SbPartNo, New_PartDesc, New_ManPartNo,New_Manuf, New_Vendor, New_RefDesi, New_Qty) select * from (select...
4
11765
by: javatopia | last post by:
Hello, I am trying to show a Crystal Reports 10 Enterprise report in an ASP.NET page (C#). I can run the report via the admin console just fine. When I try to show the report, after setting up its parameters, I get: "Value does not fall within the expected range." How are people interacting with the Crystal Report viewer to set the...
7
8382
by: rcamarda | last post by:
I wish to build a table based on values from another table. I need to populate a table between two dates from another table. Using the START_DT and END_DT, create records between those dates. I need a new column that is the days between the date and the MID_DT The data I wish to end with would look something like this: PERIOD DATE ...
0
8049
by: ssims | last post by:
I've got a GridView that's sorted by a stored procedure with ROW_NUMBER: PROCEDURE dbo.GetCalendarsByStatusIDPaged ( @startRowIndex int, @maximumRows int, @statusID int ) AS
6
3380
by: rn5a | last post by:
I am inserting records in a MS-Access database table. The data type of one of the columns named *OrderDate* in the DB table is Date/Time. This is the SQL query I am using to insert the records in the Access DB table: =============================== strSQL = "INSERT INTO Cart (CartID, ProductID, Quantity, Total, OrderDate) VALUES (CID, PID,...
17
4566
by: pbd22 | last post by:
Hi. How does one return a range of rows. I know that "Top 5" will return rows 0 - 5 but, how do I get 6 - 10? thanks
1
1074
by: arunbojan | last post by:
Hi friends, Im using a tabel with column names (timefrom, timeto, extension) .For this tabel im inserting the values using stored procedure... how to insert values if extension has range of values like (100 - 150) Following is my code: con.Open(); SqlCommand cmd=new...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.