如何从 table 的单列中获取 2 个子字符串作为 2 列

How to get 2 sub strings as 2 columns from single column of table

[{"key":"Mobile","value":"9100617634"},{"key":"Email","value":"balajirao1@ziaff.in"}]

上面的代表了table中某一列的值。 我想要 2 个名为移动和电子邮件的列,其中值分别为 9000617634,balajirao@ziraff。 如何进入sql服务器。

见下图

mobile             Email
------             ------------
9100617634         balajirao1@ziaff.in

您可以使用数据透视函数将行值转换为列值, 下面是实现的静态代码

SELECT * FROM(
   SELECT
     columns_name
   FROM
      table_name
     ) M
   PIVOT (MAX(Key) FOR table_name IN (mobile ,Email))AS  P ;

好吧,用艰苦的方式做到这一点很有趣 :)

--initial string
declare @s1 varchar(1000) = (select '[{"key":"Mobile","value":"9100617634"},{"key":"Email","value":"balajirao1@ziaff.in"}]')

--selecting first part and second part of string
declare @mobile varchar(100) = (select right(left(@s1,charindex('}',@s1)-1),len(left(@s1,charindex('}',@s1))) - charindex(':',left(@s1,charindex('}',@s1)))-1))
declare @mail varchar(100) = (select right(@s1,charindex(':',reverse(@s1))-1))

--getting rid of extra characters
set @mobile = (right(@mobile, len(@mobile) - charindex(':',@mobile)))
set @mail = (left(@mail, len(@mail) - charindex('}',reverse(@mail))))


--getting rid of double quotes
set @mobile = replace(@mobile,'"','')
set @mail = replace(@mail,'"','')

--selecting data
select 
@mobile as Mobile,
@mail as Mail

结果如下:

手机邮箱

9100617634 balajirao1@ziaff.in

SELECT 当 [MOBILE/PHONE] 喜欢 '[a-z]%' 然后 null else [MOBILE/PHONE] 以 [MOBILE/PHONE] 结束时的情况, 如果 ISNUMERIC(email)=0 那么 EMAIL 以 EMAIL 结尾 从 ( select REPLACE(SUBSTRING(RIGHT(right(left(AC.Communication,charindex('}',AC.Communication)),len(left(AC.Communication,charindex('}',AC.Communication))) - charindex(':',left(AC.Communication,charindex('}',AC.Communication)))),LEN(right(left(AC.Communication,charindex(' }',AC.Communication)),len(left(AC.Communication,charindex('}',AC.Communication))) - charindex(':',left(AC.Communication, charindex('}',AC.Communication)))))-CHARINDEX(':',right(left(AC.Communication,charindex('}',AC.Communication)),len(left (AC.Communication,charindex('}',AC.Communication))) - charindex(':',left(AC.Communication,charindex('}',AC.Communication))) ))),2,CHARINDEX(']',AC.Communication)),'"}','') AS [MOBILE/PHONE], 替换(替换(左(右(AC.Communication,charindex(':',reverse(AC.Communication))),LEN(right(AC.Communication,charindex(':',reverse(AC.Communication))))-charindex('}',reverse(right(AC.Communication,charindex(':',reverse(AC.Communication)))))),':"', ' '),'"','') 作为电子邮件

来自 DimAccountContact 作为 AC