Solved

Inserting Date field date not compatible

Posted on 2014-01-04
4
306 Views
Last Modified: 2014-01-04
I am creating a Server Agent job to insert new data into a teble from a Select statement.

I am getting the error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.

Here is the structure of the table:
create table tbl_Midnight_Stay_Admissions
([Account Number] varchar(15),
[Date of Admission] date,
[Patient Name] varchar(74),
[Attending Physician] varchar(75))
;

Here is the Insert Query:

nsert into [livedb].[PATIENTSMC\gpowers].[tbl_Midnight_Stay_Admissions]
SELECT     livedb.dbo.BarVisits.AccountNumber AS [Account Number], livedb.dbo.BarVisits.Name AS [Patient Name], CONVERT(varchar(10), DATEADD(DD, DATEDIFF(DD, 0,
                      livedb.dbo.BarVisits.AdmitDateTime), 0), 101) AS [Date of Admission], livedb.dbo.BarVisitFinancialData2.AttendProviderName AS [Attending Physician]
FROM         livedb.dbo.BarVisits LEFT OUTER JOIN
                      livedb.dbo.BarVisitFinancialData2 ON livedb.dbo.BarVisits.VisitID = livedb.dbo.BarVisitFinancialData2.VisitID LEFT OUTER JOIN
                      livedb.dbo.DMisInsurance ON livedb.dbo.BarVisits.PrimaryInsuranceID = livedb.dbo.DMisInsurance.InsuranceID
WHERE     (livedb.dbo.BarVisits.AdmitDateTime > CONVERT(DATETIME, '2013-12-31 00:00:00', 102)) AND (livedb.dbo.BarVisits.InpatientOrOutpatient = 'I') AND
                      (livedb.dbo.DMisInsurance.DefaultFinancialClassID IN ('MCR', 'MCR HMO')) AND (CONVERT(varchar(10), DATEADD(DD, DATEDIFF(DD, 0,
                      livedb.dbo.BarVisits.AdmitDateTime), 0), 101) >= DATEADD(DD, DATEDIFF(DD, 0, GETDATE()) - 3, 0))

What can I do to correct the Convert statement to create the [Date of Admission] field?

thanks

Glen
0
Comment
Question by:GPSPOW
  • 2
4 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 39756141
SELECT     livedb.dbo.BarVisits.AccountNumber AS [Account Number], livedb.dbo.BarVisits.Name AS [Patient Name],
DATEADD(DD, DATEDIFF(DD, 0,getdate()), 0) AS [Date of Admission],
livedb.dbo.BarVisitFinancialData2.AttendProviderName AS [Attending Physician]
FROM....

will work
0
 

Author Comment

by:GPSPOW
ID: 39756219
Here is modified code:

Insert into [livedb].[PATIENTSMC\gpowers].[tbl_Midnight_Stay_Admissions]
SELECT     livedb.dbo.BarVisits.AccountNumber AS [Account Number], livedb.dbo.BarVisits.Name AS [Patient Name],  
                      livedb.dbo.BarVisits.AdmitDateTime, livedb.dbo.BarVisitFinancialData2.AttendProviderName AS [Attending Physician]
FROM         livedb.dbo.BarVisits LEFT OUTER JOIN
                      livedb.dbo.BarVisitFinancialData2 ON livedb.dbo.BarVisits.VisitID = livedb.dbo.BarVisitFinancialData2.VisitID LEFT OUTER JOIN
                      livedb.dbo.DMisInsurance ON livedb.dbo.BarVisits.PrimaryInsuranceID = livedb.dbo.DMisInsurance.InsuranceID
WHERE      (livedb.dbo.BarVisits.InpatientOrOutpatient = 'I') AND
                      (livedb.dbo.DMisInsurance.DefaultFinancialClassID IN ('MCR', 'MCR HMO')) AND
                      livedb.dbo.BarVisits.AdmitDateTime >= DATEADD(DD, DATEDIFF(DD, 0, GETDATE()) - 3, 0)

I modified the target table structure to have a field AdmitDateTime with a type of datetime.

I am still getting the same error:

Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.


Glen
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39756264
see this article to know how to handle date/time (and conversion) correctly:
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html

and you need to check out which field is indeed giving the issue, then which "value" is giving the issue.
0
 

Author Closing Comment

by:GPSPOW
ID: 39756341
thanks

the article gave me insight on how to fix my problem.

Glen
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question