SQL-从字符串转换日期和/或时间时出错

SQL-从字符串转换日期和/或时间时出错,sql,sql-server,database,Sql,Sql Server,Database,我有一个datetime列,我正在检查它是否为空。我只想让它在列为空时说“无下次约会”。但是,我得到了一个错误: 从字符串转换日期和/或时间时转换失败 我不知道为什么。我尝试了几种不同的方法来编写查询,但它总是给我相同的错误 这是我的问题- SELECT t1.PatientID, t1.FullName, t1.CurrentRAF_DW, t1.NumberOfCompletedClinicVisits, t1.PreferredServiceLocation, case when t1.

我有一个datetime列,我正在检查它是否为空。我只想让它在列为空时说“无下次约会”。但是,我得到了一个错误:

从字符串转换日期和/或时间时转换失败

我不知道为什么。我尝试了几种不同的方法来编写查询,但它总是给我相同的错误

这是我的问题-

SELECT 
t1.PatientID,
t1.FullName,
t1.CurrentRAF_DW,
t1.NumberOfCompletedClinicVisits,
t1.PreferredServiceLocation,
case when t1.IsVip = 1 then 'Yes' else 'No' end as [IsVip], 
case when t1.PatientTier = 'Critical' then 'Every Three Weeks' 
     when t1.PatientTier = 'Serious' then 'Every 1 Month'
     when t1.PatientTier = 'Fair' then 'Every 2 Months'
     when t1.PatientTier = 'Good' then 'Every 3 Months'
else ' '    
end as [Cadence],
t1.PatientTier,
t2.hcc_18,
t2.hcc_19,
t2.hcc_21,
t2.hcc_22,
t2.hcc_12,
t2.hcc_136,
t2.hcc_137,
t2.hcc_108,
t2.hcc_88,
t2.hcc_111,
t2.hcc_85,
t2.hcc_55,
t1.LastCompletedClinicVisit,
case when t1.NextScheduledClinicVisit is not null then t1.NextScheduledClinicVisit else 'No Next Appointment' end as [NextScheduledVisit]
FROM vw_patient_attributes t1
INNER JOIN STG_OSHODS_DW.osh_rpt.dim_member_care_measures t2
    ON t1.PatientID = t2.emr_id

这里讨论的列是“NextScheduledClinicVisit”。

该列有时包含datetime字段,有时包含字符串


尝试将datetime强制转换为varchar。

该列有时包含datetime字段,有时包含字符串


尝试将日期时间强制转换为varchar。

首先,应该使用
coalesce()
。其次,类型应匹配:

coalesce(convert(varchar(255), t1.NextScheduledClinicVisit ),
         'No Next Appointment' 
        ) as [NextScheduledVisit]

如果
nextScheduledClinictVisite
是一个sting,那么您可能需要使用适当的转换规范(例如121)或使用
format()

,首先,您应该使用
coalesce()
。其次,类型应匹配:

coalesce(convert(varchar(255), t1.NextScheduledClinicVisit ),
         'No Next Appointment' 
        ) as [NextScheduledVisit]

如果
NextScheduledClinicVisit
是一个问题,那么您可能需要使用适当的转换规范(例如121)或使用
format()

,我将使用以下方法解决您的问题

  • 在try_convert中包装错误生成列,并查看一些示例记录
  • 如果上面成功地使用“case when[col]为null,则“formatted text 1”…”类型语句编写您的case语句,并查看一些示例记录

  • 显然,Gordon的答案是有效的,所以请使用它,但我可能会研究一下,以备将来参考。

    我将使用以下方法来解决您的问题

  • 在try_convert中包装错误生成列,并查看一些示例记录
  • 如果上面成功地使用“case when[col]为null,则“formatted text 1”…”类型语句编写您的case语句,并查看一些示例记录

  • 显然,Gordon的答案是有效的,所以请使用它,但我可能会查看以供将来参考。

    [NextScheduledVisit]
    不能是两种数据类型。Cast t1.nextscheduledclinic访问varchar,以便它和文本“无下一次约会”都是varchar数据。类似于
    CONVERT(VARCHAR(50),t1.NextScheduledClincVisit,101)
    的情况是,如果t1.NextScheduledClinicVisit不为null,则强制转换(t1.NextScheduledClinicVisit为VARCHAR(40)),否则“无下一次约会”结束为[NextScheduledVisit]列
    [NextScheduledVisit]
    不能是两种数据类型。Cast t1.nextscheduledclinic访问varchar,以便它和文本“无下一次约会”都是varchar数据。类似于
    CONVERT(VARCHAR(50),t1.NextScheduledClincVisit,101)
    case当t1.NextScheduledClinicVisit不为null时,则强制转换(t1.NextScheduledClinicVisit为VARCHAR(40)),否则“无下一次约会”结束为[NextScheduledVisit]这是一个日期时间。这起作用了。谢谢你,伙计!我试着把这个标记为“已回答”,但它说我可以在8分钟内完成,哈哈,这是约会时间。这起作用了。谢谢你,伙计!我试着把这个标记为“已回答”,但它说我可以在8分钟内完成,哈哈