Solved

Inserting Date field date not compatible

Posted on 2014-01-04
4
304 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks

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

Glen
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now