简体   繁体   English

SQL - 提取 Substring

[英]SQL - Extract Substring

Not great with SQL and am kind of stuck as to which expression to use. SQL 不太好,我对使用哪种表达方式有点坚持。 I was thinking a "case when" could help me here.我在想一个“案例”可以在这里帮助我。

So I have a messy dataset that contains a mixture of information.所以我有一个杂乱无章的数据集,其中包含混合信息。 I am trying to write a SQL statement that would extract only the rows which contain text in the following format: "ID [Int][Int][Int][Int][Int]" or "ID[Int][Int][Int][Int][Int]".我正在尝试编写一个 SQL 语句,该语句将仅提取包含以下格式文本的行:“ID [Int][Int][Int][Int][Int]”或“ID[Int][Int][整数][整数][整数]"。

ID # is always 5 integers [0-9]. ID # 始终为 5 个整数 [0-9]。 Sometimes there is no space between ID and the start of the integer.有时 ID 和 integer 的开头之间没有空格。

The logic is essentially this:逻辑本质上是这样的:

  • If (Name) contains "ID 12345" then return "ID 12345" in a separate column.如果(名称)包含“ID 12345”,则在单独的列中返回“ID 12345”。
  • If (Name) contains "ID54321" then return "ID 54321" in a separate column.如果(名称)包含“ID54321”,则在单独的列中返回“ID 54321”。

Example:例子:

Name姓名 Normalized Name规范化名称
John Smith ID 12345约翰·史密斯 ID 12345 ID 12345编号 12345
Hello ID54321 Hello你好 ID54321 你好 ID 54321编号 54321

Any assistance you could provide would be greatly appreciated.您能提供的任何帮助将不胜感激。 Thank you!谢谢!

You can use regexp for it like this:您可以像这样使用正则表达式:

SELECT *
FROM tablename
WHERE REGEXP_LIKE (Name, '(.)+ID( )+[0-9]+(.)+');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM