替换String在Postgresql中包含多个字符串

时间:2017-02-09 07:35:38

标签: postgresql postgresql-9.1

人员的送货地址中包含多个文本,例如在此4行消费者地址中。

     |         Address Line                 |
     |--------------------------------------|
     |APARTMENT 5800 springfield GARDENS CIR|
     |--------------------------------------|
     |APT 5800 springfield GARDENS CIR      |
     |--------------------------------------|
     |2567 south pay street                 |
     |--------------------------------------|
     |Office 222 Market , VA 048946         |

如何添加包含某些字符的其他列,例如Address Line包含" Apartment"或者" APT"将成为"公寓"并且Address Line包含" Street"将成为"家庭" ,Address Line包含" Office"将成为"办公大楼"和"未知" Address Line未包括在内。

这是PostgreSql

附带的查询后的所需表格
         |         Address Line                 |      Location      |
         |--------------------------------------|--------------------|
         |APARTMENT 5800 springfield GARDENS CIR|   Apartement       |
         |--------------------------------------|--------------------|
         |APT 5800 springfield GARDENS CIR      |   Apartement       |
         |--------------------------------------|--------------------|
         |2567 south pay street                 |   Household        |
         |--------------------------------------|--------------------|
         |Office 222 Market , VA 048946         |   Office building  |
         |--------------------------------------|--------------------|

这是我的疑问:

 SELECT ORDER_TABLE.guest_email,
        CASE WHEN ORDER_SHIPPINGADDRESS.LINE1 IN ('Apartement', 'apt') Then 'Apartement'
        WHEN ('street') then 'household'
        WHEN ('Office') then 'Office Building'
        ELSE 'Location Unknown' END as Location

到目前为止,我使用Case When进行了试用,但结果是这样的错误

ERROR: argument of CASE/WHEN must be type boolean, not type record

是否有任何功能可以执行上面所需的表格?感谢

1 个答案:

答案 0 :(得分:1)

您应该在每个WHEN

中重复ORDER_SHIPPINGADDRESS.LINE1 IN
SELECT ORDER_TABLE.guest_email,
    CASE WHEN ORDER_SHIPPINGADDRESS.LINE1 IN ('Apartement', 'apt') Then 'Apartement'
         WHEN ORDER_SHIPPINGADDRESS.LINE1 IN ('street') then 'household'
         WHEN ORDER_SHIPPINGADDRESS.LINE1 IN ('Office') then 'Office Building'
    ELSE 'Location Unknown' END as Location