如何动态获取子字符串

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

How to get substring dynamically

问题

如何动态获取子串,我想将一个单词拆分为两部分,一部分是字符串,另一部分是数字,

AA112 这是我的字符串,所以从 SQL 查询中

SELECT substr(thisColumn, 1,2) AS String from thisTable

我在查询中获取到了 AA,我传递的索引(2)是动态的,我从某个值获取,所以我现在能够获取字符串,现在我想获取数字+1,就像它是112,然后变成113

我尝试过多种查询,但没有成功,我尝试使用 JavaScript 的子串,但没有成功。

示例输出
AAA123 -> AAA124

AA4 -> AA5

BBBB8 -> BBBB9

英文:

How can I get substring dynamically, I want to split one word into two parts one is a string and other to no,

AA112 this is my string so from sql query

SELECT substr(thisColumn, 1,2) AS String from thisTable

I am getting AA in the query the index(2) I am pasing is dynamic I am getting from some value, So here I am able to get the string now I want to get the number +1 like it is 112 then -> 113

I have tried several queries but it is not working, I have tried doing it with javascript substring but it didn't work out.

Sample output
AAA123 -> AAA and 124

AA4 -> AA and 5

BBBB8 -> BBBB and 9

答案1

得分: 2

<!-- 开始代码段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->

const input = "AA112";
var numberPattern = /\d+/g;
const number = input.match(numberPattern)[0];
const text = input.replace(number, "");

console.log(text);   // AA
console.log(number); // 112

const incremented = parseInt(number) + 1;
console.log(incremented);

<!-- 结束代码段 -->
英文:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->

const input=&quot;AA112&quot;;
var numberPattern = /\d+/g;
const number = input.match(numberPattern)[0];
const text = input.replace(number,&quot;&quot;);

console.log(text);   //AA
console.log(number); //112

const incremented = parseInt(number) + 1;
console.log(incremented);

<!-- end snippet -->

答案2

得分: 1

使用LOCATEIFLEASTSUBSTRING在两个子查询上可以实现您正在寻找的输出结果。

SELECT VAL, 
       SUBSTRING(VAL,1,FIRSTNUM-1),
       SUBSTRING(VAL,FIRSTNUM,999)+1 
FROM
(SELECT VAL,LEAST(V0+0,V1+0,V2+0,V3+0,V4+0,V5+0,V6+0,V7+0,V8+0,V9+0) FIRSTNUM FROM
(SELECT VAL,
       IF(LOCATE(0,VAL)=0,999,LOCATE(0,VAL)) V0,
       IF(LOCATE(1,VAL)=0,999,LOCATE(1,VAL)) V1,
       IF(LOCATE(2,VAL)=0,999,LOCATE(2,VAL)) V2,
       IF(LOCATE(3,VAL)=0,999,LOCATE(3,VAL)) V3,
       IF(LOCATE(4,VAL)=0,999,LOCATE(4,VAL)) V4,
       IF(LOCATE(5,VAL)=0,999,LOCATE(5,VAL)) V5,
       IF(LOCATE(6,VAL)=0,999,LOCATE(6,VAL)) V6,
       IF(LOCATE(7,VAL)=0,999,LOCATE(7,VAL)) V7,
       IF(LOCATE(8,VAL)=0,999,LOCATE(8,VAL)) V8,
       IF(LOCATE(9,VAL)=0,999,LOCATE(9,VAL)) V9 FROM mytable) A ) B;

在这里,我使用LOCATE来查找值中数字出现的位置,然后使用REPLACE将该值更改为999,如果返回值为0的话。这对于下一步使用LEAST的操作至关重要,因为如果我将其保留为0,它将返回全部0。我使用LEAST的原因是,如果您看一下fiddle中的第四个数据示例,它是AAAAA321。这将返回如下网格中的结果:

+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   VAL    | V0  | V1  | V2  | V3  | V4  | V5  | V6  | V7  | V8  | V9  |
+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| AAA123   | 999 |   4 |   5 |   6 | 999 | 999 | 999 | 999 | 999 | 999 |
| AA4      | 999 | 999 | 999 | 999 |   3 | 999 | 999 | 999 | 999 | 999 |
| BBBB8    | 999 | 999 | 999 | 999 | 999 | 999 | 999 | 999 |   5 | 999 |
| AAAAA321 | 999 |   8 |   7 |   6 | 999 | 999 | 999 | 999 | 999 | 999 | <---这个
| AAA45    | 999 | 999 | 999 | 999 |   4 |   5 | 999 | 999 | 999 | 999 |
+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

第一个数字出现实际上是'3',但因为我构建的查询从0到9运行,所以我将始终得到前面的第一个数字(在这种情况下,'1'是第8次出现),尽管它位于中间或最后位置。这就是为什么LEAST将使用LOCATE函数正确确定哪个数字实际上是最先出现的原因。

在最后的外部查询中,我使用LEAST函数的结果(赋值为FIRSTNUM)作为SUBSTRING的定义值。第一个子字符串将用作减去1以获取字符串的结束位置,并且在第二个子字符串中,我将其用作获取数字的第一个位置(我将位置结束为999)。

Fiddle链接:https://www.db-fiddle.com/f/F15cxiYJxGcozYUbQukeB/5

编辑:我意识到我不能使用REPLACE,因为如果位置是'10',它将将'0'替换为'999',这将使其变为'1999'。所以我使用了IF

英文:

Using LOCATE, IF,LEAST and SUBSTRING over two sub-query can achieve the output result you're looking for.

SELECT VAL, 
       SUBSTRING(VAL,1,FIRSTNUM-1),
       SUBSTRING(VAL,FIRSTNUM,999)+1 
FROM
(SELECT VAL,LEAST(V0+0,V1+0,V2+0,V3+0,V4+0,V5+0,V6+0,V7+0,V8+0,V9+0) FIRSTNUM FROM
(SELECT VAL,
       IF(LOCATE(0,VAL)=0,999,LOCATE(0,VAL)) V0,
       IF(LOCATE(1,VAL)=0,999,LOCATE(1,VAL)) V1,
       IF(LOCATE(2,VAL)=0,999,LOCATE(2,VAL)) V2,
       IF(LOCATE(3,VAL)=0,999,LOCATE(3,VAL)) V3,
       IF(LOCATE(4,VAL)=0,999,LOCATE(4,VAL)) V4,
       IF(LOCATE(5,VAL)=0,999,LOCATE(5,VAL)) V5,
       IF(LOCATE(6,VAL)=0,999,LOCATE(6,VAL)) V6,
       IF(LOCATE(7,VAL)=0,999,LOCATE(7,VAL)) V7,
       IF(LOCATE(8,VAL)=0,999,LOCATE(8,VAL)) V8,
       IF(LOCATE(9,VAL)=0,999,LOCATE(9,VAL)) V9 FROM mytable) A ) B;

Here I use LOCATE to find the location of the number occurrence in the value then REPLACE to change the value to 999 if it returns 0. This is essential for the next operation using LEAST because if I leave it 0, it'll return all 0 instead. The reason I use LEAST is because if you look at the 4th data example in the fiddle it's AAAAA321. This will return the result in grid like the following:

+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   VAL    | V0  | V1  | V2  | V3  | V4  | V5  | V6  | V7  | V8  | V9  |
+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| AAA123   | 999 |   4 |   5 |   6 | 999 | 999 | 999 | 999 | 999 | 999 |
| AA4      | 999 | 999 | 999 | 999 |   3 | 999 | 999 | 999 | 999 | 999 |
| BBBB8    | 999 | 999 | 999 | 999 | 999 | 999 | 999 | 999 |   5 | 999 |
| AAAAA321 | 999 |   8 |   7 |   6 | 999 | 999 | 999 | 999 | 999 | 999 | &lt;---this
| AAA45    | 999 | 999 | 999 | 999 |   4 |   5 | 999 | 999 | 999 | 999 |
+----------+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

The first number occurrence is actually '3' but because I construct the query running from 0-9, I'll always get the first number in front (in this case '1' is the 8th occurrence) despite it being on the middle or last position. That's why LEAST will correctly take which number is actually located first using LOCATE function.

In the last outer query, I use the result from the LEAST function (assigned as FIRSTNUM) as the defining value for the SUBSTRING. The first substring I use it as the end location subtract by 1 to get the strings and in the second substring I use it as the first location to get the number (I end the location with 999).

Fiddle: https://www.db-fiddle.com/f/F15cxiYJxGcozYUbQukeB/5

Edit: I realized I cannot use REPLACE because if the location is like 10, it will replace 0 to 999 which makes it become 1999 instead. So I use IF.

huangapple
  • 本文由 发表于 2020年1月6日 14:41:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607736.html
匿名

发表评论

匿名网友

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

确定