【问题标题】:NULL fields after executing LENGTH(email_address)执行 LENGTH(email_address) 后的 NULL 字段
【发布时间】:2019-01-02 00:52:03
【问题描述】:

我遇到了一个问题,我想获得字段的最大长度,因为我想知道是否存在超过为该字段建立的大小的字段,例如:varchar2(20)

我用的是oracle 11g,看看这个:

https://i.stack.imgur.com/4DdWM.jpg

所以,我做了这个查询:

    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
  , hl.address1
  , hl.address2
  , hl.address3
  , hl.address4
  , hl.city
  , hl.province
  , hl.postal_code
  , hcas.status                                
  , DECODE( hcas.attribute5, 'PUP', 'Y', 'N' ) 
  , 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')
        AND length (phone_number) > 40
        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 = ''
;

我设置了这个条件 "AND length (phone_number) > 40" 但它不起作用,因为它显示空字段,为什么?

看看这个: IMAGE

还有,这种情况可以吗?

hcp ON hcas.party_site_id = hcp.owner_table_id 

你能帮我解决这个问题吗?

PS:我正在使用 SQL DEVELOPER 和 oracle 数据库 11g

【问题讨论】:

  • 请不要张贴(链接到)表格结构、代码或查询结果的图片,使用文字。

标签: sql oracle11g


【解决方案1】:

您修改查询的方式看起来不太好。您在子查询中添加了WHERE 子句,用于从表hz_contact_point 中恢复用户电子邮件和电话号码。现在看起来子查询没有返回任何内容,并且结果中的相关字段为NULL(如果您使用INNER JOIN而不是LEFT JOIN,则根本没有结果),

如果您只需要控制或格式化输出,请不要修改子查询:它可能会破坏逻辑。使用主查询。例如,如果电话号码超过 40 个字符,则以下示例将电话号码替换为 NULL

SELECT
     hp.party_name
    , hca.account_number
    ...
    , CASE WHEN LENGTH(hcp.phone_number) > 40 THEN NULL ELSE hcp.phone_number END
    , hcp.email_address
...

【讨论】:

  • 感谢您的回复!伙计,当我尝试过滤表以查找与 account_number 相关的所有信息时出现此错误(在 SQL DEVELOPER 中)imgur.com/a/ddgz2DAWHY?
  • 我该如何解决?
【解决方案2】:

您离开了别名为hcp 的子查询连接,因此如果hcp 中没有匹配的行与之前内部连接的其他表的某些行匹配,则hcp 中的值对于这些行是NULL

但如果phone_number 被声明为varchar2(40),您将找不到任何带有length(phone_number) > 40 的电话号码。如果有人试图插入它们,它们就会被拒绝。

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多