几天前我的SUBSTRING
/ CHARINDEX
查询中断了,我正在努力了解原因。
我正在尝试在“ Appt。Date:”和“ Appt。Time:”之间选择字符(日期),例如:
Status: Future Appointment Appt. Date:12/14/18 Appt. Time:9:30am
Status: Obtaining Results Appt. Date:10/05/18 Appt. Time:4:00
Status: Appt. Date:8/28/2018 Appt. Time: 9:15am
使用此查询:
select ltrim(
rtrim(
SUBSTRING([Order Comments],
CHARINDEX('Appt. Date:', [Order Comments]) + LEN('Appt. Date:'),
CHARINDEX('Appt. Time',[Order Comments], CHARINDEX('Appt. Date:', [Order Comments])) -
(CHARINDEX('Appt. Date:', [Order Comments]) + LEN('Appt. Date:'))
)
)
) as 'OrderApptDate'
FROM [HIRS_Tools].[dbo].[OMT_BOD]
WHERE [Order Comments] like 'status:%'
SQL错误:
传递给LEFT或SUBSTRING函数的无效长度参数。
我应该采用其他方法来选择日期字符,还是当前查询只是一个问题?任何帮助将不胜感激。
答案 0 :(得分:0)
首先,非常感谢大家发布的帖子。即使他们都没有给我确切的答案,他们都给了我一个答案。特别是HABO和3N1GM4!
最终,我发现“申请日期”和“申请时间”字段并不总是完全相同。有Appt的组合。日期:&Appt。时间,约日期-&Appt。时间,日期和日期时间...都是从HABO的字段长度表达式中发现的。 因此,我添加了一个case语句,以针对每个[Appt Date / Appt Time]组合字段长度进行调整。这解决了所有问题,并给了我足够的杠杆作用,使我的用户可以清理剩余的记录并实现更好的数据输入工作流程。
新查询如下:
select
case
when CHARINDEX('Appt. Time',[Order Comments], CHARINDEX('Appt. Date:', [Order Comments])) - (CHARINDEX('Appt. Date:', [Order Comments]) + LEN('Appt. Date:')) <15
and [Type]='Procedure' AND [Order Comments] like 'status:%' and [Order Comments] like '%Appt. Time%'
then ltrim(rtrim(SUBSTRING([Order Comments], CHARINDEX('Appt. Date:', [Order Comments]) + LEN('Appt. Date:'), CHARINDEX('Appt. Time:',[Order Comments], CHARINDEX('Appt. Date:', [Order Comments])) - (CHARINDEX('Appt. Date:', [Order Comments]) + LEN('Appt. Date:')))))
when CHARINDEX('Appt. Time',[Order Comments], CHARINDEX('Appt. Date:', [Order Comments])) - (CHARINDEX('Appt. Date:', [Order Comments]) + LEN('Appt. Date:')) <15
and [Type]='Procedure' AND [Order Comments] like 'status:%' and [Order Comments] like '%Appt Time%'
then ltrim(rtrim(SUBSTRING([Order Comments], CHARINDEX('Appt Date:', [Order Comments]) + LEN('Appt Date:'), CHARINDEX('Appt Time:',[Order Comments], CHARINDEX('Appt Date:', [Order Comments])) - (CHARINDEX('Appt Date:', [Order Comments]) + LEN('Appt Date:')))))
when (CHARINDEX('Appt. Time',[Order Comments], CHARINDEX('Appt. Date -', [Order Comments])) - (CHARINDEX('Appt. Date -', [Order Comments]) + LEN('Appt. Date -'))) <15
and [Type]='Procedure' AND [Order Comments] like 'status:%' and [Order Comments] like '%Appt. Date -%'
then ltrim(rtrim(SUBSTRING([Order Comments], CHARINDEX('Appt. Date -', [Order Comments]) + LEN('Appt. Date -'), CHARINDEX('Appt. Time:',[Order Comments], CHARINDEX('Appt. Date -', [Order Comments])) - (CHARINDEX('Appt. Date -', [Order Comments]) + LEN('Appt. Date -')))))
when (CHARINDEX('Appt. Time',[Order Comments], CHARINDEX('Appt. Date ', [Order Comments])) - (CHARINDEX('Appt. Date ', [Order Comments]) + LEN('Appt. Date '))) <15
and [Type]='Procedure' AND [Order Comments] like 'status:%' and [Order Comments] like '%Appt. Date %'
then ltrim(rtrim(SUBSTRING([Order Comments], CHARINDEX('Appt. Date ', [Order Comments]) + LEN('Appt. Date '), CHARINDEX('Appt. Time:',[Order Comments], CHARINDEX('Appt. Date ', [Order Comments])) - (CHARINDEX('Appt. Date ', [Order Comments]) + LEN('Appt. Date ')))))
else [Date - Due] end as 'OrderApptDate'
FROM [HIRS_Tools].[dbo].[OMT_BOD]
WHERE ([Order Comments] like 'status:%')
再次感谢您的帮助,希望以后有人对它有用。