分割动态字符串为级别

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

Split a dynamic string as levels

问题

我有一个包含动态字符串的表格,其长度根据以下示例更改

  1. 字符串1A.B.C:D.E.F:G.H.I:J
  2. 字符串2A.B.C:D

我需要拆分字符串,得到以下设计:

  1. level_0
  2. level_1
  3. level_2
  4. level_N
  5. level_Attribute

例如,对于字符串2,设计将为

  1. level_0: A
  2. level_1: B
  3. level_2: C
  4. level_Attribute: D

我能够使用以下代码拆分并创建列,并将每个值分配给一个级别

  1. DECLARE @str VARCHAR(100)='A.B.C:D.E.F:G.H.I:J';
  2. set @str=REPLACE(@str,':','.')
  3. DECLARE @JsonArray NVARCHAR(MAX)=CONCAT('[["',REPLACE(@str,'.','","'),'"]]');
  4. SELECT @str AS TheOriginal
  5. ,@JsonArray AS TransformedToJSON
  6. ,ValuesFromTheArray.*
  7. FROM OPENJSON(@JsonArray)
  8. WITH(Level_0 VARCHAR(100) '$[0]'
  9. ,Level_1 VARCHAR(100) '$[1]'
  10. ,Level_2 VARCHAR(100) '$[2]'
  11. ,Level_3 VARCHAR(100) '$[3]'
  12. ,Level_4 VARCHAR(100) '$[4]'
  13. ,Level_5 VARCHAR(100) '$[5]'
  14. ,Level_6 VARCHAR(100) '$[6]'
  15. ,Level_7 VARCHAR(100) '$[7]'
  16. ,Level_8 VARCHAR(100) '$[8]'
  17. ,Level_Attribute VARCHAR(100) '$[9]'
  18. ) ValuesFromTheArray

我的问题是当字符串较短时,最后一项未添加到level_attribute。

英文:

I have table containing dynamics string that changes in length as per the example below

  1. string 1: A.B.C:D.E.F:G.H.I:J
  2. string 2: A.B.C:D

I need to split the string to get to the following design :

  1. level_0
  2. level_1
  3. level_2
  4. level_N
  5. level_Attribute

so for example for String 2 the design will be

  1. level_0: A
  2. level_1: B
  3. level_2: C
  4. level_Attribute: D

I was able to split and create the colunm and assign each value to a level using the following

  1. DECLARE @str VARCHAR(100)='A.B.C:D.E.F:G.H.I:J';
  2. set @str=REPLACE(@str,':','.')
  3. DECLARE @JsonArray NVARCHAR(MAX)=CONCAT('[["',REPLACE(@str,'.','","'),'"]]');
  4. SELECT @str AS TheOriginal
  5. ,@JsonArray AS TransformedToJSON
  6. ,ValuesFromTheArray.*
  7. FROM OPENJSON(@JsonArray)
  8. WITH(Level_0 VARCHAR(100) '$[0]'
  9. ,Level_1 VARCHAR(100) '$[1]'
  10. ,Level_2 VARCHAR(100) '$[2]'
  11. ,Level_3 VARCHAR(100) '$[3]'
  12. ,Level_4 VARCHAR(100) '$[4]'
  13. ,Level_5 VARCHAR(100) '$[5]'
  14. ,Level_6 VARCHAR(100) '$[6]'
  15. ,Level_7 VARCHAR(100) '$[7]'
  16. ,Level_8 VARCHAR(100) '$[8]'
  17. ,Level_Attribute VARCHAR(100) '$[9]'
  18. ) ValuesFromTheArray

My problem is when the string is short , the last item is not added to the level_attribute.

答案1

得分: 0

-- 1 - create table
创建表格
create table sample_levels
(
sample_data varchar(128)
);

-- 2 - add data
添加数据
insert into sample_levels values ('A.B.C:D.E.F:G.H.I:J');
insert into sample_levels values ('A.B.C:D');

-- get level + attribute
获取级别和属性
select
substring(backwards, 1, charindex(',', backwards) - 1) as attribute,
reverse(substring(backwards, charindex(',', backwards) + 1, len(backwards) - charindex(',', backwards) )) as levels
from
(
select
reverse(replace(replace(sample_data, '.', ','), ':', ',')) as backwards
from sample_levels
) as d

我在SQL Fiddle中创建了这个示例。这是用于运行SQL代码的链接。

分割动态字符串为级别

英文:

There are many ways to solve this problem. I am choosing to leave the level as one big string and the attribute as another string. Lets create the table and insert data.

  1. -- 1 - create table
  2. create table sample_levels
  3. (
  4. sample_data varchar(128)
  5. );
  6. -- 2 - add data
  7. insert into sample_levels values ('A.B.C:D.E.F:G.H.I:J');
  8. insert into sample_levels values ('A.B.C:D');

The trick is to convert to comma delimited string and reverse and parse.

  1. -- get level + attribute
  2. select
  3. substring(backwards, 1, charindex(',', backwards) - 1) as attribute,
  4. reverse(substring(backwards, charindex(',', backwards) + 1, len(backwards) - charindex(',', backwards) )) as levels
  5. from
  6. (
  7. select
  8. reverse(replace(replace(sample_data, '.', ','), ':', ',')) as backwards
  9. from sample_levels
  10. ) as d

I did this example in SQL Fiddle. Here is the link to play with the SQL code.

http://sqlfiddle.com/#!18/d16f4/1/0

分割动态字符串为级别

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

发表评论

匿名网友

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

确定