打印列中字符串的部分

huangapple go评论54阅读模式
英文:

print only part of string in a column

问题

想要打印列中字符串的某一部分并忽略剩余部分,包括特殊字符,该如何实现

| A        | B              |
| -------- | -------------- |
| abc      | kadala         |
| def      | kade           |
| de       | kad            |

尝试过 SUBSTRING_INDEX(B,'=', -1) AS B

期望输出

| A        | B              |
| -------- | -------------- |
| abc      | kadala         |
| def      | kade           |
| de       | kad            |
英文:

I wanted to print only one part of string in column and ignore the remaining stuff including special characters, how can i acheive it

| A        | B              |
| -------- | -------------- |
| abc      | zet=kadala;    |
| def      | zet=kade; None |
| de       | zet=kad; None:81 |

Tried SUBSTRING_INDEX(B,'=', -1) AS B

expected output

| A        | B              |
| -------- | -------------- |
| abc      | kadala         |
| def      | kade           |
| de       | kad            |

答案1

得分: 2

If you want to go the SUBSTRING_INDEX() route, then you will need to call it twice:

<!-- language: sql -->

SELECT A, SUBSTRING_INDEX(SUBSTRING_INDEX(B, ';', 1), '=', -1) AS B
FROM yourTable;

On MySQL 8+, you could also use a regex replacement:

<!-- language: sql -->

SELECT A, REGEXP_REPLACE(B, '^.?=|;.$', '') AS B
FROM yourTable;

英文:

If you want to go the SUBSTRING_INDEX() route, then you will need to call it twice:

<!-- language: sql -->

SELECT A, SUBSTRING_INDEX(SUBSTRING_INDEX(B, &#39;;&#39;, 1), &#39;=&#39;, -1) AS B
FROM yourTable;

On MySQL 8+, you could also use a regex replacement:

<!-- language: sql -->

SELECT A, REGEXP_REPLACE(B, &#39;^.*?=|;.*$&#39;, &#39;&#39;) AS B
FROM yourTable;

答案2

得分: 1

你可以使用SUBSTRING_INDEX()函数从列B中提取所需的子字符串。SUBSTRING_INDEX()函数返回一个字符串中指定分隔符之前或之后的子字符串。

以下是一个示例的SQL查询,应该可以给你期望的输出:

SELECT A, SUBSTRING_INDEX(SUBSTRING_INDEX(B, '=&', -1), ';', 1) AS B
FROM your_table_name;

在上面的查询中,我们使用了两个嵌套的SUBSTRING_INDEX()函数来从列B中提取所需的子字符串。

内部的SUBSTRING_INDEX()函数返回最后一个'=&'字符之后的子字符串,在第一行中给我们'kadala',在第二行中给我们'kade',在第三行中给我们'kad'。

外部的SUBSTRING_INDEX()函数返回第一个';'字符之前的子字符串,从'kadala','kade'和'kad'后面删除了剩余的字符。

注意:将'your_table_name'替换为你实际表的名称。

英文:

You can use the SUBSTRING_INDEX() function to extract the required substring from column B. The SUBSTRING_INDEX() function returns a substring from a string before or after a specified delimiter.

Here's an example SQL query that should give you the expected output:

SELECT A, SUBSTRING_INDEX(SUBSTRING_INDEX(B, &#39;=&#39;, -1), &#39;;&#39;, 1) AS B
FROM your_table_name;

In the above query, we are using two nested SUBSTRING_INDEX() functions to extract the required substring from column B.

The inner SUBSTRING_INDEX() function returns the substring after the last occurrence of the '=' character, which gives us 'kadala' in the first row, 'kade' in the second row, and 'kad' in the third row.

The outer SUBSTRING_INDEX() function returns the substring before the first occurrence of the ';' character, which removes the remaining characters after 'kadala', 'kade', and 'kad'.

Note: Replace 'your_table_name' with the actual name of your table.

答案3

得分: 0

为了提取列B中'='字符后的子串并删除分号(';')后的任何附加字符,您可以使用SUBSTRING_INDEX函数两次。

以下是一个可以实现预期输出的SQL查询示例:

SELECT A, TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(B, '=', -1), ';', 1)) AS B
FROM table_name;

此查询将选择列A以及列B的子串,该子串从最后一个'='字符后开始,并在第一个';'字符出现时结束。TRIM函数用于从生成的字符串中删除任何前导或尾随空格。

此查询的输出应该是一个只包含所需子串的新表格中的列B。

英文:

To extract the substring after the '=' character in column B and remove any additional characters after the semicolon (;), you can use the SUBSTRING_INDEX function twice.

Here's an example SQL query that should achieve the expected output:

SELECT A, TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(B, &#39;=&#39;, -1), &#39;;&#39;, 1)) AS B
FROM table_name;

This query will select column A and a substring of column B that starts after the last occurrence of the '=' character and ends at the first occurrence of the ';' character. The TRIM function is used to remove any leading or trailing spaces from the resulting string.

The output of this query should be a new table with only the desired substring in column B.

huangapple
  • 本文由 发表于 2023年3月20日 23:00:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75791929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定