在CASE中比较字符串

时间:2019-09-03 21:01:30

标签: sql postgresql null comparison operators

我有一个包含character varying个字段的表格-

Bill    Render     Refer        Supervise
        1194772160  
        1538359872  
        1194772160  
        1104026103  
        1104026103  
                                1831124015
        1740237197  1740237197  1740237197

下面的查询为什么返回空字符串-

SELECT
    CASE
        WHEN render != bill THEN render
    END AS renderUnique

2 个答案:

答案 0 :(得分:4)

如果要将空值比较为相等,请使用function imageSizeAfterRotation(size, degrees) { degrees = degrees % 180; if (degrees < 0) { degrees = 180 + degrees; } if (degrees >= 90) { size = [ size[1], size[0] ]; degrees = degrees - 90; } if (degrees === 0) { return size; } const radians = degrees * Math.PI / 180; const width = (size[0] * Math.cos(radians)) + (size[1] * Math.sin(radians)); const height = (size[0] * Math.sin(radians)) + (size[1] * Math.cos(radians)); return [ width, height ]; } // USAGE: imageSizeAfterRotation([ 200, 80 ], 30) // [ 213.20508075688775, 169.28203230275508 ]

IS [NOT] DISTINCT FROM

请注意,SELECT CASE WHEN render IS DISTINCT FROM bill THEN render END AS renderUnique 大致意味着x与y不同。真相表应该有帮助:

x IS DISTINCT FROM y

答案 1 :(得分:1)

由于null列的所有行都包含bill,因此比较:

render != bill

等效于:

render != null

返回null,因此不是true,并且case语句返回null
您可以将其更改为:

select 
(CASE WHEN coalesce(render, '') != coalesce(bill, '') THEN render END) as renderUnique