Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 4729

Conversion failed when converting date and/or time from character string.

$
0
0

Hi,

This is my code and I don't know why this error keeps coming out : PS : I did cursor to execute query.Th error showed is bold:

DECLARE RegCreatedDate CURSOR FOR
SELECT DISTINCT (CONVERT(NVARCHAR,CreatedDate,103)) 
FROM CA_Registration WHERE Month(CreatedDate)= @paMonthIn AND YEAR(CreatedDate)=@paYearIn

OPEN RegCreatedDate
FETCH NEXT FROM RegCreatedDate INTO @RegCreatedDate
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #TempDiagnosisTable


 Select   @CompanyName AS CompanyName,@RegistrationNumber AS RegistrationNo,@ParentCompanyName AS ParentCompanyName,
   @MOHFacilityCode AS MOHFacilityCode,@CompanyState as DisState,DateName( month , DateAdd( month ,CAST(@paMonthIn AS INT) , -1 )) AS DisMonth,@paYearIn as DisYear,
   dbo.CA_Registration.ID as ID, dbo.CA_Registration.MRN as MRN, dbo.CA_Registration.VisitNumber as VisitNumber,
dbo.CA_Patient.DOB as DOB, DATEDIFF(yy,CA_Patient.DOB,GETDATE()) as Age, 
dbo.CA_GenderMaster.Description as Sex, dbo.CA_DoctorMaster.DoctorName as PrimaryDoctor, 
dbo.CA_Registration.RegDate as RegistrationDate, 
                dbo.CA_DischargeDetails.DischargeDate as DODischarge, DATEDIFF(dd,ca_registration.regdate,CA_DischargeDetails.DischargeDate) as DaysStayed, case when CA_DischargeDetails.DischargeReasonID =7 then 'YES' else 'NO' END as DischargeTypeAsDeath,
                dbo.CA_PnICD.Diagnosis as Diagnosis, dbo.CA_PnICD.ICDPrimaryID as ICDPrimary, dbo.CA_PnICD.ICDDeathID as ICDDeath, dbo.CA_PnICD.ICDSecondaryID as ICDSecondary
 
 FROM           dbo.CA_Registration LEFT OUTER JOIN
     dbo.CA_PnICD ON dbo.CA_Registration.ID = dbo.CA_PnICD.ID LEFT OUTER JOIN
                      dbo.CA_DoctorMaster ON dbo.CA_Registration.PrimaryDoctorID = dbo.CA_DoctorMaster.ID LEFT OUTER JOIN
                      dbo.CA_DischargeDetails ON dbo.CA_Registration.MRN = dbo.CA_DischargeDetails.MRN AND 
                      dbo.CA_Registration.VisitNumber = dbo.CA_DischargeDetails.VisitNumber RIGHT OUTER JOIN
                      dbo.CA_Patient LEFT OUTER JOIN
                      dbo.CA_GenderMaster ON dbo.CA_Patient.GenderID = dbo.CA_GenderMaster.ID ON dbo.CA_Registration.MRN = dbo.CA_Patient.MRN 
                        where CA_Registration.ChargeTypeID in (1,2) and MONTH(ca_registration.regdate)=@paMonthIn and YEAR(ca_registration.regdate)=@paYearIn
AND dbo.CA_Registration.ID = 
(SELECT MAX(ID) FROM CA_Registration WHERE MRN = dbo.CA_Registration.MRN 
AND (CONVERT(NVARCHAR,CreatedDate,103)) = @RegCreatedDate)


FETCH NEXT FROM RegCreatedDate INTO @RegCreatedDate

END
CLOSE RegCreatedDate
DEALLOCATE RegCreatedDate


--RUN THE TABLE---

SELECT * FROM #TempDiagnosisTable ORDER BY ID

END



Viewing all articles
Browse latest Browse all 4729

Trending Articles