获取所有长度超过25个字符的记录(LENGTH(hcp.phone_number)> 25)

时间:2019-01-24 01:47:26

标签: sql

我正在尝试根据以下条件从我的查询中获取所有记录:

(LENGTH(hcp.phone_number) > 25)

实际上我正在使用这种条件:

CASE WHEN LENGTH(hcp.phone_number) > 25 then null else hcp.phone_number END

但是对于单个帐号,当 hcp.phone_number 字段大于25时,它只显示NULL。

我的查询工作正常,但是我想对其进行修改,以获取 ALL 符合以下条件的帐号:

LENGTH(hcp.phone_number) > 25

这是我的查询

SELECT
    hp.party_name                              
  , hca.account_number
  , hca.cust_account_id                        
 -- , hcsu.LOCATION customer_site_name
  , hcas.cust_acct_site_id                     
  , hcp.phone_number
  , hcp.email_address
  , CASE WHEN LENGTH(hcp.phone_number) > 25 then null else hcp.phone_number END
  , hl.address1
  , hl.address2
  , hl.address3
  , hl.address4
  , hl.city
  , hl.province
  , hl.postal_code
  , hcas.status  as hcas_status                               
  , DECODE( hcas.attribute5, 'PUP', 'Y', 'N' ) 
  , hca.status  as hca_status                             
FROM apps.hz_cust_accounts hca
INNER JOIN apps.hz_cust_acct_sites_all hcas ON hca.cust_account_id = hcas.cust_account_id
INNER JOIN apps.hz_party_sites hps ON hcas.party_site_id = hps.party_site_id
INNER JOIN apps.hz_locations hl ON hps.location_id = hl.location_id
INNER JOIN apps.hz_parties hp ON hps.party_id = hp.party_id
LEFT JOIN (
        SELECT
            owner_table_id
          , max(case when contact_point_type = 'PHONE' then phone_number end) phone_number
          , max(case when contact_point_type = 'EMAIL' then email_address end) email_address
        FROM hz_contact_points
        WHERE status = 'A'
        AND primary_flag = 'Y'
        AND owner_table_name = 'HZ_PARTY_SITES'
        AND contact_point_type IN ('EMAIL','PHONE')
        GROUP BY 
            owner_table_id
    ) hcp ON hcas.party_site_id = hcp.owner_table_id 
WHERE hcas.status = 'A'
AND hps.status = 'A'
AND hca.status = 'A'
AND hca.account_number = ''
;

您能帮我吗?

1 个答案:

答案 0 :(得分:1)

我认为您应该将所选列更改为此

, CASE WHEN LENGTH(hcp.phone_number) > 25 then hcp.phone_number END

并删除条件AND hca.account_number = ''

如果要在结果中过滤掉所有长度为LENGTH(hcp.phone_number)<= 25的内容,请在WHERE子句:AND LENGTH(hcp.phone_number) > 25中添加条件。