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 does this SQL2005 script error?

Author  Topic 

stamford
Starting Member

47 Posts

Posted - 2014-05-20 : 12:41:03
The EVENT_DATE field has a datatype of datetime in all the relevant tables, and the fields EVENT_ID and CARE_ID are integers, but I get an error. Username is a varchar. How do I get round this?


Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query.



INSERT INTO newNBOCAP_CARE_PLANS_WITH
SELECT CARE_ID, MAX(EVENT_DATE) AS EVENT_DATE, MAX(EVENT_ID) AS EVENT_ID, Username FROM
(SELECT i.CARE_ID, EVENT_DATE, EVENT_ID, Username FROM newNBOCAP_CARE_PLANS i
JOIN
(SELECT CARE_ID, MIN(EVENT_DATE) FirstTRDate
FROM newNBOCAP_CARE_PLANS
WHERE EVENT_TYPE IN ('BR','CH','TE','SU')
GROUP BY CARE_ID) FirstTP
ON FirstTP.CARE_ID = i.CARE_ID
WHERE
i.EVENT_TYPE = 'CARE_PLAN' AND
i.EVENT_DATE <= FirstTP.FirstTRDate) a
GROUP BY CARE_ID, Username

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-05-20 : 13:58:52
Show us the DDL for newNBOCAP_CARE_PLANS_WITH and the tables involved in the SELECT. You've got a data type mismatch somewhere.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-05-20 : 15:57:49
It is good practice to use an explicit column list for your INSERT INTO clause. Perhaps the ordinal_position of the columns in your table doesn't match column order of your SELECT clause. the explicit column list would fix that.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -