Teradata 字符串操作(第二个空间)
我很难解决这个看似简单的任务:
目的: 创建一个消除中间初始
示例
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
挑战在于确保您的姓名格式一致。 (
Last_Name, Give_Name Middle_Initial
) 如果是,那么您可以使用递归 SQL 来解决此问题。以下 SQL 将采用Given_Name Last_Name
并返回Last_Name
。您也许可以调整它来完成您的特定任务。 (我的示例数据的格式不一致,因此我一直在尝试查找第二次(或第三次)出现的空白字符。)另一种选择是实现返回最右边的 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 takeGiven_Name Last_Name
and returnLast_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.)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.