我试图弄清楚如何修改查询以仅显示满足条件的值。
我的查询:
SELECT
name = r.RscMaster_Name_Ch
,Start_date = a.assign_from_da
,End_date = a.Assign_Thru_Da
FROM DW_WFTS.dbo.Assign_Tbl a
LEFT JOIN DW_WFTS.dbo.Resource_Master_Tbl r ON r.RscMaster_No_In = a.RscMaster_No_In
WHERE
(
a.Assign_Thru_Da >= GETDATE()
OR a.Assign_Thru_Da IS NULL
OR
(
a.Assign_From_Da >= GETDATE()
AND a.Assign_Thru_Da >= GETDATE()
)
OR
(
a.Assign_From_Da <= GETDATE()
AND a.Assign_Thru_Da <= GETDATE()
)
)
AND
(
(
a.Assign_From_Da LIKE '0%'
OR a.Assign_From_Da LIKE '00%'
OR a.Assign_From_Da LIKE '000%'
)
OR
(
a.Assign_Thru_Da LIKE '0%'
OR a.Assign_Thru_Da LIKE '00%'
OR a.Assign_Thru_Da LIKE '000%'
)
);
因此,如果有一个或多个用户使用了错误的日期格式-输入为'0YYY'或'00YY'或'000Y'的任何年份-我希望查询显示名称并且仅显示错误的日期,例如
name Start_date end_date
John 0019-12-01
Jane 0020-01-05
Mike 0019-01-05 0019-12-01
我不确定如何从选择中省略正确的日期。请帮忙。
答案 0 :(得分:2)
如果您的担忧只是一年中的前导零
“输入为'0YYY'或'00YY'或'000Y'的任何年份”
并且它们的类型为DATE
,那么您将到达那里:
SELECT
name = r.RscMaster_Name_Ch
,Start_date = a.assign_from_da
,End_date = a.Assign_Thru_Da
FROM DW_WFTS.dbo.Assign_Tbl a
LEFT JOIN DW_WFTS.dbo.Resource_Master_Tbl r ON r.RscMaster_No_In = a.RscMaster_No_In
where
a.assign_from_da < '1000-01-01'
or a.Assign_Thru_Da < '1000-01-01'
如果您有更多的日期规则,则可以扩展where子句
答案 1 :(得分:1)
您可以使用ISDATE()函数。对于无效日期的字符表达式,它返回零。
SELECT
name = r.RscMaster_Name_Ch
,Start_date = a.assign_from_da
,End_date = a.Assign_Thru_Da
FROM DW_WFTS.dbo.Assign_Tbl a
LEFT JOIN DW_WFTS.dbo.Resource_Master_Tbl r ON r.RscMaster_No_In = a.RscMaster_No_In
WHERE ISDATE( a.assign_from_da ) = 0
OR
ISDATE( a.Assign_Thru_Da ) = 0
答案 2 :(得分:0)
SQL Server中的DATE
类型支持从0001-01-01
到9999-12-31
的值。 SMALLDATETIME
类型允许从1900-01-01
到2079-06-06
的值。
您可以使用TRY_CAST()
尝试转换为SMALLDATETIME
,然后将其转换回日期。从1900-01-01
到2079-06-06
之外的所有值都将返回NULL。
SELECT
name = r.RscMaster_Name_Ch
,Start_date = CAST(TRY_CAST(a.assign_from_da AS SMALLDATETIME) AS DATE)
,End_date = CAST(TRY_CAST(a.Assign_Thru_Da AS SMALLDATETIME) AS DATE)
FROM DW_WFTS.dbo.Assign_Tbl a
LEFT JOIN DW_WFTS.dbo.Resource_Master_Tbl r ON r.RscMaster_No_In = a.RscMaster_No_In
WHERE
(
ISNULL(a.Assign_Thru_Da, DATEADD(DAY,1,GETDATE())) >= GETDATE()
OR
(
a.Assign_From_Da >= GETDATE()
AND a.Assign_Thru_Da >= GETDATE()
)
OR
(
a.Assign_From_Da <= GETDATE()
AND a.Assign_Thru_Da <= GETDATE()
)
)
然后您可以像下面这样将此查询插入您的sp_send_dbmail
呼叫中:
BEGIN
IF @count > 0
BEGIN
EXEC msdb.dbo.sp_send_dbmail
--@profile_name='emailFrsdata2',
@recipients=N'won.chang@montgomerycountymd.gov',
@subject = 'WFTS - Incorrect Assignment Dates found',
@query = N'SELECT
name = r.RscMaster_Name_Ch
,Start_date = CAST(TRY_CAST(a.assign_from_da AS SMALLDATETIME) AS DATE)
,End_date = CAST(TRY_CAST(a.Assign_Thru_Da AS SMALLDATETIME) AS DATE)
FROM DW_WFTS.dbo.Assign_Tbl a
LEFT JOIN DW_WFTS.dbo.Resource_Master_Tbl r ON r.RscMaster_No_In = a.RscMaster_No_In
WHERE
(
ISNULL(a.Assign_Thru_Da, DATEADD(DAY,1,GETDATE())) >= GETDATE()
OR
(
a.Assign_From_Da >= GETDATE()
AND a.Assign_Thru_Da >= GETDATE()
)
OR
(
a.Assign_From_Da <= GETDATE()
AND a.Assign_Thru_Da <= GETDATE()
)
)
ORDER BY r.RscMaster_Name_Ch',
@attach_query_result_as_file = 0
END
END