Solved

Inserting Date field date not compatible

Posted on 2014-01-04
4
308 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

738 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