我的Sql代码获取消息245,级别16,状态1,行1转换失败转换varchar值'早上8:45'到数据类型int

时间:2016-05-10 08:34:16

标签: sql-server-2008

Msg 245,Level 16,State 1,Line 1 转换varchar值时转换失败< 8:45 AM'到数据类型int。 我的脚本是

 Declare @SpecimenId bigint = 0, @script varchar(MAX);
Declare @StartTime int = (Select Convert(int,SUBString(START_TIME,0,CHARINDEX(':', START_TIME)) - 12) From APPT 
                            Where APPT._ID=112601)

                            --select @StartTime
Set @script = 
'SELECT distinct    PTLAST_NAME + '', '' + PT.FIRST_NAME As PATIENT_NAME, 
            dbo.FormatDate(PT.DOB,''MM/dd/yyyy'') As DOB, 
            (Convert(varchar, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END)) + ''/'' +
            Convert(varchar, (DATEDIFF(m, DATEADD(yy, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END), DOB), APPT_DATE) - CASE WHEN DAY(DOB) > DAY(APPT_DATE) THEN 1 ELSE 0 END)) + ''/'' +
            Convert(varchar, (DATEDIFF(d, (DATEADD(m, (DATEDIFF(m, DATEADD(yy, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END), DOB), APPT_DATE) - CASE WHEN DAY(DOB) > DAY(APPT_DATE) THEN 1 ELSE 0 END), (DATEADD(yy, (DATEDIFF(yy, DOB, APPT_DATE) - CASE WHEN (MONTH(DOB) > MONTH(APPT_DATE)) OR (MONTH(DOB) = MONTH(APPT_DATE) AND DAY(DOB) > DAY(APPT_DATE)) THEN 1 ELSE 0 END), DOB)))), APPT_DATE)))) As PatAge,
            PT.Org_Id, PT.ADDRESS1 As PatAddress,
            PT.LAST_NAME As LastName,PT.FIRST_NAME As FirstName,
            PT.MIDDLE_NAME As MiddleName,
            PHY.LAST_NAME + '', '' + PHY.FIRST_NAME As Physician,
            dbo.FormatDate(APPT.APPT_DATE,''MM/dd/yyyy'') As CollectedDate,
            (Case when SUBString(START_TIME,0,CHARINDEX('':'', START_TIME)) < 12 Then Convert(varchar,START_TIME) + Convert(varchar,'' AM'') ELSE' +Convert(varchar,@StartTime)+  '+ SUBString(START_TIME,3,6) + '' PM'' END) As CollectedTime,

            (Case When PT.GENDER =''1'' Then ''M'' Else ''F'' End) As Gender,PT.CITY,LST.State_Short_Name As State ' + 
            (Case When @SpecimenId = 0 Then ', 0  As SpeciId' Else ', (Case When Tbl_Pat.Speci_Id > 0 Then Tbl_Pat.Speci_Id Else '''' End) As SpeciId' End) +
' FROM         PT INNER JOIN
                      APPT ON PT.PAT_ID = APPT.PAT_ID INNER JOIN
                      PHY ON APPT.PHY_ID = PHY.PHY_ID INNER JOIN
                      LST ON PT.STATE = LST.STATE_ID ' + 
                      (Case When @SpecimenId = 0 Then '' Else 'LEFT OUTER JOIN  Tbl_Pat ON APPT.APPT_ID = Tbl_Pat.Speci_ApptId' End)
 + ' WHERE    (APPT.APPT_ID = 112601) ' +
(Case When @SpecimenId = 0 Then '' Else 'AND (Tbl_Pat.Speci_Id = ' + Convert(varchar, @SpecimenId) + ')' End)
exec (@script)

请任何人帮助或纠正我的剧本

0 个答案:

没有答案