我的日期格式在sql中是“MMddyy”..当我试图通过我做下面的日期转换错误,但我不知道我需要做什么?条件失败:我的WarrantReceivedDate应该小于startDate,DateWarrantExecuted应该> = StartDate?任何帮助?提前致谢
declare @startDate VARCHAR(6) = '010119'
declare @endDate VARCHAR(6) = '013119'
DECLARE @MyTable TABLE (
WarrantIssuingAuthority varchar(100),
CountType varchar(100),
CountRecords int
)
insert into @MyTable (WarrantIssuingAuthority, CountType, CountRecords)
select WarrantIssuingAuthority, 'WARRANTS BEGINNING OF MONTH', count(*)
from [PM].WarrantInformation
where TypeOfWarrantIssued!='COM'
and HowExecuted!='R' and HowExecuted!='U'
and DeletedIndicator!='D'
and DeletedIndicator!='P'
and CONVERT(VARCHAR(10), CAST((RIGHT(WarrantReceivedDate,2) + LEFT(WarrantReceivedDate,4)) AS DATE),112) < CONVERT(VARCHAR(10), CAST((RIGHT(@startDate,2) + LEFT(@startDate,4)) AS DATE),112)
and (DateWarrantExecuted='' or (CONVERT(VARCHAR(10), CAST((RIGHT(DateWarrantExecuted,2) + LEFT(DateWarrantExecuted,4)) AS DATE),112) >= CONVERT(VARCHAR(10), CAST((RIGHT(@startDate,2) + LEFT(@startDate,4)) AS DATE),112)))
group by WarrantIssuingAuthority
select * from @MyTable order by WarrantIssuingAuthority
我们无法看到您的表的定义或其中包含的数据,但可能存在无法正确转换为日期的值。例如,可能有一个值093118
,它是一个有效的varchar
值,但不是有效的date
。