如何在SQL中指示日期为空

时间:2019-05-31 14:29:28

标签: sql sql-server

使用以下代码,我希望检索ClosedDateTimeT字段的值为空的记录,但它仅显示具有值的记录。我在这里做错什么了吗?

SELECT DISTINCT
    t.ticketid,
    t.Sub,
    i.status, 
    i.OwnedByTeam,
    i.OwnedByTeamT,
    i.TypeOptions,
    i.ClosedDateTimeT,
    i.CompletedDateTime,
    i.TitleT 
FROM
    ticket AS t 
INNER JOIN
    Incident AS i ON t.TicketID = i.IncidentID
WHERE
    CAST(t.Sub AS DATE) >= '01-01-2019' 
    AND CAST(t.Sub AS DATE ) <= '05-30-2019'
    AND i.Subcategory = 'Content Request'
    AND (i.TypeOptions = 'New/Initial Submission' OR
         i.TypeOptions = 'Am/Re')
    AND ((i.OwnedByTeamT LIKE '%C - CA%' 
          AND i.TitleT LIKE '%B Build%' 
          AND i.ClosedDateTimeT IS NOT NULL) OR
         (i.OwnedByTeamT LIKE '%C - CA%'
          AND i.TitleT LIKE '%B Build%'
          AND i.ClosedDateTimeT IS NULL) OR
         (i.OwnedByTeamT LIKE '%E - Cl%' 
          AND i.TitleT LIKE '%OP B V%'))

3 个答案:

答案 0 :(得分:0)

不太确定您要面对的问题是什么,但是可以对查询进行一些简化和格式化。注意,我也在使用符合ANSI的日期文字。

Select distinct t.ticketid
    , t.Sub
    , i.status
    , i.OwnedByTeam
    , i.OwnedByTeamT
    , i.TypeOptions
    , i.ClosedDateTimeT
    , i.CompletedDateTime
    , i.TitleT 
From ticket as t 
inner join Incident as i on t.TicketID=i.IncidentID
where 
    CAST(t.Sub AS DATE) >= '20190101' 
    AND CAST(t.Sub AS DATE ) <= '20190530'
    AND i.Subcategory = 'Content Request'
    AND i.TypeOptions in ('New/Initial Submission', 'Am/Re')
    AND 
    (
        (
            i.OwnedByTeamT like '%C - CA%' 
            AND 
            i.TitleT LIKE '%B Build%'
        )
        OR
        (
            i.OwnedByTeamT like '%E - Cl%' 
            AND 
            i.TitleT LIKE '%OP B V%'
        )
    )

答案 1 :(得分:0)

你不是这个意思吗?

SELECT DISTINCT t.ticketid
    ,t.Sub
    ,i.STATUS
    ,i.OwnedByTeam
    ,i.OwnedByTeamT
    ,i.TypeOptions
    ,i.ClosedDateTimeT
    ,i.CompletedDateTime
    ,i.TitleT
FROM ticket AS t
INNER JOIN Incident AS i ON t.TicketID = i.IncidentID
WHERE i.ClosedDateTimeT IS NULL
    AND CAST(t.Sub AS DATE) >= '01-01-2019'
    AND CAST(t.Sub AS DATE) <= '05-30-2019'
    AND i.Subcategory = 'Content Request'
    AND (
        i.TypeOptions = 'New/Initial Submission'
        OR i.TypeOptions = 'Am/Re'
        )
    AND (
        (
            i.OwnedByTeamT LIKE '%C - CA%'
            AND i.TitleT LIKE '%B Build%'
            )
        OR (
            i.OwnedByTeamT LIKE '%E - Cl%'
            AND i.TitleT LIKE '%OP B V%'
            )
        )

答案 2 :(得分:0)

通过您的评论,我将删除,只需从您的where子句中删除该日期字段即可。我还删除了一块重复了i.OwnedByTeamT的代码,例如'%C-CA%'和i.TitleT类似'%B Build%'

Select distinct t.ticketid,
 t.Sub,
 i.status, 
 i.OwnedByTeam,
 i.OwnedByTeamT,
 i.TypeOptions,
 i.ClosedDateTimeT,
 i.CompletedDateTime,
 i.TitleT 

From ticket as t inner join Incident as i on  
t.TicketID=i.IncidentID

where 
CAST(t.Sub AS DATE) >= '01-01-2019' 
AND CAST(t.Sub AS DATE ) <= '05-30-2019'
AND i.Subcategory = 'Content Request'
AND (i.TypeOptions='New/Initial Submission' OR
 i.TypeOptions='Am/Re')
AND (
(i.OwnedByTeamT like '%C - CA%' AND i.TitleT LIKE '%B Build%' 
)
OR (
i.OwnedByTeamT like '%E - Cl%' AND i.TitleT LIKE '%OP B V%')
)