comparing null in SQL comparisons

前端 未结 5 455
梦谈多话
梦谈多话 2021-01-24 09:56

I am creating a front end with VB6 and my database is Sybase. Using DSN I have created some small exe\'s to populate reports in grid. It was working fine.

However if I u

相关标签:
5条回答
  • 2021-01-24 09:59

    You cannot use when user_id <> Null. You must use user_id Is Null or user_id Is Not Null. Anything = or <> to Null results in Unknown which is treated as false for the Case expression.

    0 讨论(0)
  • 2021-01-24 10:03

    Just so you know the count(seqnum) as Answered is not the correct definition for Aspect UIP inbound calls answered. If you check the field labeled SwitchDispId it could have a 1 or 2 in it which would equate to an abandon call. Hence not a answered call. I do see you are using user_id not null to get answered calls but I just wanted to make you aware of that.

    Also you can join the acdcalldetail table to the service table to get names that look more like what the business is used to seeing like this:

    SELECT 
         Service_c
        ,SUM(CASE WHEN acd.SwitchDispID IN (1,2) THEN 1 ELSE 0 END as Abandons
        ,SUM(CASE WHEN acd.user_id is not null THEN 1 ELSE 0 END as Answered
    FROM acdcalldetail acd
    JOIN service s
        ON s.service_id = acd.service_id
        AND s.sourceid = acd.sourceid
    WHERE acd.CallStartDt between '20170501' AND '20170530'
    AND s.Service_id NOT IN  (37,39,47,51,57,58,96,215,374,375)
    GROUP BY 
        s.Service_c
    
    "select datepart (hh, callstartdt) as Hour, " _
        & " count(seqnum) as Anaswered," _
        & " sum(case when user_id <> NULL then 1 else 0 end) as answered_calls ," _
        & " sum(case when user_id <> NULL and  datediff (ss, callstartdt, QueueEndDt) <= 20 then 1 else 0 end) , " _
        & " sum(case when user_id = NULL then 1 else 0 end), " _
        & " sum(case when user_id <> NULL and datediff (ss, callstartdt, QueueEndDt) <= 20 then 1 else 0 end)  / count(seqnum), " _
        & " sum(Case when user_id <> NULL then 1 else 0 end ) / count(seqnum) from acdcalldetail " _
        & " where callstartdt between '" & fromDt & "' and '" & toDt & "' " _
        & " and service_id not in (37,39,47,51,57,58,96,215,374,375) " _
        & " group by datepart (hh, callstartdt) " _
        & " order by datepart (hh, callstartdt)"
    
    0 讨论(0)
  • 2021-01-24 10:11

    Nicholas, I agree with you in that comparisons against null should always be false, however I tried the following code on an ASE 15.0.3 server and got a surprising result

    declare @xx int
    select @xx = null
    select @xx
    select 1 where null = @xx
    

    It return 1 for the second select which I was not expecting..

    0 讨论(0)
  • 2021-01-24 10:21

    General Rule: any operation involving NULL yields NULL. All comparisons involving NULL fail, regardless of whether or not the test is positive ('==') or negative ('<>'). The sole exception being explicit tests for nullity via IS [NOT] NULL or use of COALESCE()/ISNULL().

    0 讨论(0)
  • 2021-01-24 10:22

    I guess for sysbase it is the same as for sql-server.

    There is a setting to switch between (old?) sybase default

    set ansi_nulls off
    
    select case when null = null then 1 else 0 end
    -- returns 1
    

    and ansi behaviour.

    set ansi_nulls on
    
    select case when null = null then 1 else 0 end
    -- returns 0
    

    Today the question is hardly which setting is more elegant, but with which setting causes more trouble.

    0 讨论(0)
提交回复
热议问题