Returning the original value if it doesn't match any of the when_expressions

让人想犯罪 __ 提交于 2020-01-05 07:45:18

问题


I have a line on my SQL export field, and I am trying to remove return a blank value in a field if it exactly matches "AMA" in the last name. However, when I export the file with the current code, and names without "AMA" as the last name are fully blanked out.

,Max(
    Orders.ShipFirstName
    + ' ' +  
        CASE Orders.ShipLastName
            WHEN 'AMA' THEN ''
        END
) As ShipFullName

Right now, if someone is "Ray Miller", ShipFullName will be returned as blank. But if their name is "Ray AMA", then their name is returned as "Ray". I want it so "Ray Miller" is returned when AMA is not found in the last name field.


回答1:


To avoid having a trailing blank, I would do this:

max(
    case 
        when Orders.ShipLastName = 'AMA' then Orders.ShipFirstName
        else Orders.ShipFirstName + ' ' + Orders.ShipLastName
    end
) as ShipFullName



回答2:


CASE Orders.ShipLastName
    WHEN 'AMA' THEN ''
    ELSE Orders.ShipLastName
END



回答3:


,Max(
    Orders.ShipFirstName
    + ' ' +  IF(Orders.ShipLastName = 'AMA', '', Orders.ShipLastName)
) As ShipFullName


来源:https://stackoverflow.com/questions/14513751/returning-the-original-value-if-it-doesnt-match-any-of-the-when-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!