Teradata 字符串操作(第二个空间)

发布于 2024-12-10 16:31:06 字数 439 浏览 2 评论 0原文

我很难解决这个看似简单的任务:

目的: 创建一个消除中间初始

示例

Name
Smith, John A
Jane, Mary S

的查询我想要这样的输出:

Name
Smith, John
Jane, Mary

有关如何使用 Teradata SQL 执行此操作的任何提示

我相信我解决了该问题,尽管方式非常糟糕:

SELECT SUBSTR('SMITH, JOHN A', 0, (POSITION(' ' IN 'SMITH, JOHN A') + (POSITION(' ' IN SUBSTR('SMITH, JOHN A',(POSITION(' ' IN 'SMITH, JOHN A'))+ 1,50)))))

I'm having great difficulty solving this seemingly easy task:

Purpose:
Create a query that eliminates the middle Initial

Example

Name
Smith, John A
Jane, Mary S

I would like an output such as this:

Name
Smith, John
Jane, Mary

Any tips on how to do this with Teradata SQL

I believe I solved the issue, albeit in a very poor way:

SELECT SUBSTR('SMITH, JOHN A', 0, (POSITION(' ' IN 'SMITH, JOHN A') + (POSITION(' ' IN SUBSTR('SMITH, JOHN A',(POSITION(' ' IN 'SMITH, JOHN A'))+ 1,50)))))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愚人国度 2024-12-17 16:31:06
select a,
substr(a,1,index(a,' '))|| substr(trim(substr(a,index(a,' '))),1,index(trim(substr(a,index(a,' '))),' ')),
substr(trim(substr(a,index(a,' '))),index(trim(substr(a,index(a,' '))),' ')) last_name
from a
select a,
substr(a,1,index(a,' '))|| substr(trim(substr(a,index(a,' '))),1,index(trim(substr(a,index(a,' '))),' ')),
substr(trim(substr(a,index(a,' '))),index(trim(substr(a,index(a,' '))),' ')) last_name
from a
又怨 2024-12-17 16:31:06

挑战在于确保您的姓名格式一致。 (Last_Name, Give_Name Middle_Initial) 如果是,那么您可以使用递归 SQL 来解决此问题。以下 SQL 将采用 Given_Name Last_Name 并返回 Last_Name。您也许可以调整它来完成您的特定任务。 (我的示例数据的格式不一致,因此我一直在尝试查找第二次(或第三次)出现的空白字符。)

WITH RECURSIVE cte (FullName, DelimPosition, RecursionLevel, Element, Remainder) AS
(
  SELECT FullName
       , 0 AS DelimPosition_
       , 0 
       , CAST('' AS VARCHAR(128))
       , FullName
    FROM MyDatabase.Persons

  UNION ALL

  SELECT FullName
       , CASE WHEN POSITION(' ' IN Remainder) > 0
              THEN POSITION(' ' IN Remainder)
              ELSE CHARACTER_LENGTH(Remainder)
         END DelimPosition_
       , RecursionLevel + 1
       , SUBSTRING(Remainder FROM 0 FOR DelimPosition_ + 1)
       , SUBSTRING(Remainder FROM DelimPosition_ + 1)
    FROM cte
   WHERE DelimPosition_ > 1
     AND RecursionLevel < 3 -- Set max depth
)
SELECT FullName
     , CASE WHEN POSITION('&' IN Element) = 0
            THEN Element 
            ELSE NULL
       END AS LastName
  FROM cte c
 WHERE RecursionLevel > 2
 ORDER BY FullName;

另一种选择是实现返回最右边的 n 个字符的 UDF的一个字符串。 (例如RIGHT(FullName, n)

如果格式不一致,那么我们必须考虑其他不太优雅的选项。

希望这有帮助。

The challenge is making sure your names are consistently formatted. (Last_Name, Given_Name Middle_Initial) If they are then you may be able to solve this with recursive SQL. The following SQL would take Given_Name Last_Name and return Last_Name. You may be able to tweak it to accomplish your specific task. (My sample data was not consistently formatted so I was stuck trying to find the second (or third) occurrence of a white space character.)

WITH RECURSIVE cte (FullName, DelimPosition, RecursionLevel, Element, Remainder) AS
(
  SELECT FullName
       , 0 AS DelimPosition_
       , 0 
       , CAST('' AS VARCHAR(128))
       , FullName
    FROM MyDatabase.Persons

  UNION ALL

  SELECT FullName
       , CASE WHEN POSITION(' ' IN Remainder) > 0
              THEN POSITION(' ' IN Remainder)
              ELSE CHARACTER_LENGTH(Remainder)
         END DelimPosition_
       , RecursionLevel + 1
       , SUBSTRING(Remainder FROM 0 FOR DelimPosition_ + 1)
       , SUBSTRING(Remainder FROM DelimPosition_ + 1)
    FROM cte
   WHERE DelimPosition_ > 1
     AND RecursionLevel < 3 -- Set max depth
)
SELECT FullName
     , CASE WHEN POSITION('&' IN Element) = 0
            THEN Element 
            ELSE NULL
       END AS LastName
  FROM cte c
 WHERE RecursionLevel > 2
 ORDER BY FullName;

Another option would be to implement a UDF that returns the rightmost n characters of a string. (e.g.RIGHT(FullName, n))

If the formatting is not consistent then we have to look at other less graceful options.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文