【问题标题】:How to exclude everything after an @ in SQL?如何在 SQL 中排除 @ 之后的所有内容?
【发布时间】:2015-06-03 23:17:14
【问题描述】:

我是 sql 新手,我正在编写一个 sql 脚本来选择数据,结果将以 .csv 格式显示。对于其中一个字段,我只需要为列表中的每个人选择电子邮件地址中 @ 之前的内容。我不想更新表中的记录。

例如:john.doe@yahoo.com

我只需要选择 john.doe

我在执行此操作时需要帮助。我在 Linux 环境中使用 sqlplus。

这就是我在脚本中拥有的内容。我仍然需要帮助才能获得所需的输出。

(选择 nvl(c.email_email_address, ' ')

    from email c, person a
    where c.email_pidm = a.person_pidm and
          PERSON.ID = a.person_id and
          c.email_emal_code = 'EMPL' and
          c.email_status_ind = 'A' and
          c.rowid = (select max(b.rowid)
                                   from email b
                                   where b.email_pidm = a.person_pidm and
                                         b.email_emal_code = 'EMP'
                                   and b.email_status_ind = 'A')
                                                                    ) "Employee_Email_Address",
SELECT SUBSTR(email_email_address, 0, INSTR(email_email_address, '@')-1) 
--(select nvl(c.email_email_address, ' ')
        from email c, person a
        where c.email_pidm = a.person_pidm and
              PERSON.ID = a.person_id and
              c.email_emal_code = 'EMP' and
              c.email_status_ind = 'A' and
              c.rowid = (select max(b.rowid)
                                       from email b
                                       where b.email_pidm = a.person_pidm and
                                             b.email_emal_code = 'EMPL'
                                       and b.email_status_ind = 'A')
                                                                        ) "Username"

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    SELECT SUBSTR('test@example.com', 1, INSTR('test@example.com', '@')-1) FROM DUAL;

    输出:

    测试

    【讨论】:

      【解决方案2】:

      你可以这样做:

      select SUBSTR("test@testdomain.com", 1, INSTR("test@testdomain.com", "@")-1)
      from dual
      

      【讨论】:

        【解决方案3】:
        SELECT SUBSTR(FIELD_NAME, 0, INSTR(FIELD_NAME, '@')-1) FROM TABLE_NAME
        

        SUBSTR 的工作方式如下:

        SUBSTR( string, start_position, [ length ] )
        

        长度参数是 ...INSTR(FIELD_NAME, '@')-1... 为您输入的。

        INSTR 告诉您正在查找或找到的字符在字符串(或字段)中的什么位置。您必须将该结果设为 -1,因此“@”不会显示在您的返回值中。

        【讨论】:

        • 虽然 DB 引擎会自动为您将 0 转换为 1,但可能值得注意的是,在 Oracle 中,字符串是从 1 开始的,而不是从 0 开始的。
        猜你喜欢
        • 1970-01-01
        • 2021-06-28
        • 1970-01-01
        • 2012-11-13
        • 2017-12-31
        • 1970-01-01
        • 1970-01-01
        • 2011-03-30
        • 2018-12-11
        相关资源
        最近更新 更多