Solved

Inserting Date field date not compatible

Posted on 2014-01-04
4
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 143

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

691 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