如何根据列中的信息在SQL中创建新列?

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

How to create a new column in SQL based on information in a column?

问题

我正在尝试在一个表格中查找消息代码,并认为如果我创建一个新列,用相应的公式指定列中的每个代码会更容易。

因此,我正在尝试在表格中创建一个名为MessageCode的新列,该列指定"MessageCode"之后的每个数字,但我不确定如何做到这一点。最终,我希望得到一个表格,基本上返回Id、Title、Enabled、Message Code和Actions。

如果您需要更多信息,请让我知道。此外,我正在使用SSMS。

ID      Title     Enabled Mark Colour       Condition                                                     Action
1914	ABW: 012   	1	    -256	       {"Operator":1,"Children":[{"Operator":1,"LeftOperand":}[{"ActionType":1,"Results":[{"ExcludeApplicationGroups":[5],"IncludeEZY":false,"StartDate":"2022-07-08T00:00:00","MessageCode":9109,"Ordinal":32,"Level":2,"Status":2,"LocalisedMessages":[{"Message":"<p>Passengers are advised to hold travel insurance that covers Covid-19.</p>"},{"LanguageId":50,"Message":"<p>Passagiers wordt aangeraden een reisverzekering af te sluiten die het Covid-19 dekt.</p>"},{"LanguageId":75,"Message":"<p><strong>&nbsp;</strong>Il est conseillé aux passagers d'avoir une assurance voyage qui couvre le Covid-19.</p>"},{"LanguageId":156,"Message":"<p><strong>&nbsp;</strong>Se aconseja a los pasajeros que tengan un seguro de viaje que cubra Covid-19.</p>"},{"LanguageId":140,"Message":"<p><strong>&nbsp;</strong>Os passageiros são aconselhados a possuir um seguro de viagem que cubra o Covid-19.</p>"},{"LanguageId":141,"Message":"<p><strong>&nbsp;</strong>Aconselha-se os passageiros a possuírem um seguro de viagem que cubra o Covid-19.</p>"}]}]}]
英文:

I am trying to search message codes within 1 table and think it would be easier if I create a new column that specifies each code within the column with the corresponding formula.

So I am trying to create a new column within the table called MessageCode which specifies each number after "MessageCode": but I am not sure how to do this. In the end I want a table that basically gives back Id, Title, Enabled, Message Code and Actions.

If you need further information please let me know.Also I am using SSMS.

ID      Title     Enabled Mark Colour       Condition                                                     Action
1914	ABW: 012   	1	    -256	       {&quot;Operator&quot;:1,&quot;Children&quot;:[{&quot;Operator&quot;:1,&quot;LeftOperand&quot;:}	[{&quot;ActionType&quot;:1,&quot;Results&quot;:[{&quot;ExcludeApplicationGroups&quot;:[5],&quot;IncludeEZY&quot;:false,&quot;StartDate&quot;:&quot;2022-07-08T00:00:00&quot;,&quot;MessageCode&quot;:9109,&quot;Ordinal&quot;:32,&quot;Level&quot;:2,&quot;Status&quot;:2,&quot;LocalisedMessages&quot;:[{&quot;Message&quot;:&quot;&lt;p&gt;Passengers are advised to hold travel insurance that covers Covid-19.&lt;/p&gt;&quot;},{&quot;LanguageId&quot;:50,&quot;Message&quot;:&quot;&lt;p&gt;Passagiers wordt aangeraden een reisverzekering af te sluiten die het Covid-19 dekt.&lt;/p&gt;&quot;},{&quot;LanguageId&quot;:75,&quot;Message&quot;:&quot;&lt;p&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;Il est conseill&#233; aux passagers d&#39;avoir une assurance voyage qui couvre le Covid-19.&lt;/p&gt;&quot;},{&quot;LanguageId&quot;:156,&quot;Message&quot;:&quot;&lt;p&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;Se aconseja a los pasajeros que tengan un seguro de viaje que cubra Covid-19.&lt;/p&gt;&quot;},{&quot;LanguageId&quot;:140,&quot;Message&quot;:&quot;&lt;p&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;Os passageiros s&#227;o aconselhados a possuir um seguro de viagem que cubra o Covid-19.&lt;/p&gt;&quot;},{&quot;LanguageId&quot;:141,&quot;Message&quot;:&quot;&lt;p&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;Aconselha-se os passageiros a possu&#237;rem um seguro de viagem que cubra o Covid-19.&lt;/p&gt;&quot;}]}]}]

答案1

得分: 1

你可以尝试使用以下查询来读取“Action”列中的值:

SELECT ID, Title, Enabled, MarkColour, Condition,
       JSON_VALUE(Action, '$[0].Results[0].MessageCode') AS MessageCode
FROM <YourTableName>
英文:

You can try to use the below query to read the value from Action column

SELECT ID, Title, Enabled, MarkColour, Condition,
       JSON_VALUE(Action, &#39;$[0].Results[0].MessageCode&#39;) AS MessageCode
FROM &lt;YourTableName&gt;

huangapple
  • 本文由 发表于 2023年2月8日 19:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385278.html
匿名

发表评论

匿名网友

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

确定