我正在尝试检查我作为日期值转换的VARCHAR
值,然后检查转换值是否大于另一个值是否按预期工作。
我附上了s.EFFECTIVE_DATE
的图片,其大于t.StatusEffectiveDate
以及为什么它会出现在我的结果集中。这是我的问题。
这是我的代码,如果问题出现在WHERE子句下面的粗体AND语句:
DECLARE @peims2016SnapshotDate date = '2016-10-28'
SELECT t.StudentID, t.Status, s.status, t.StatusEffectiveDate, s.effective_date, t.enddate, @peims2016SnapshotDate as peimssnapshotdate, CONVERT(VARCHAR(10), DATEADD(day, -1, t.Enddate),101) as NewEndDate
FROM Mcsfwisdtest.FRA.dbo.vw_HHSurvey s
INNER JOIN Fwisddw.dbo.Focus_Export_Econ_DisadvantagedHHS_TEST t
ON s.CustomerID = t.StudentID
AND s.SiteID = LocationID
WHERE CAST(t.Enddate as date) > @peims2016SnapshotDate
**AND CAST(s.effective_date as date) > CAST(t.StatusEffectiveDate as date)**
AND t.[Status] = 'PAID' AND s.[Status] != 'PAID'
OR (t.[Status] = 'REDUCED' AND s.[Status] = 'FREE')
ORDER BY t.StudentID asc
答案 0 :(得分:4)
您需要使用正确的样式(在您的情况下为样式date
)转换为101
:
DECLARE @peims2016SnapshotDate date = '2016-10-28'
SELECT t.StudentID,
t.Status,
s.status,
t.StatusEffectiveDate,
s.effective_date,
t.enddate,
@peims2016SnapshotDate as peimssnapshotdate,
CONVERT(VARCHAR(10),DATEADD(DAY,-1,t.Enddate),101) as NewEndDate
FROM Mcsfwisdtest.FRA.dbo.vw_HHSurvey s
INNER JOIN Fwisddw.dbo.Focus_Export_Econ_DisadvantagedHHS_TEST t
ON s.CustomerID = t.StudentID
AND s.SiteID = LocationID
WHERE CAST(t.Enddate as date) > @peims2016SnapshotDate
AND CONVERT(date,s.effective_date,101) > CONVERT(date,t.StatusEffectiveDate,101)
AND ((t.[Status] = 'PAID' AND s.[Status] != 'PAID')
OR (t.[Status] = 'REDUCED' AND s.[Status] = 'FREE'))
ORDER BY t.StudentID asc;