IT Blog

  • Blog
  • Technology
    • Architecture
    • CMS
    • CRM
    • Web
    • DotNET
    • Python
    • Database
    • BI
    • Program Language
  • Users
    • Login
    • Register
    • Forgot Password?
  • ENEN
    • ENEN
Experience IT
In a World of Technology, People Make the Difference.
  1. 首页
  2. Database
  3. SQL Server
  4. 正文

Useful sql script for developers

2020年10月20日 50点热度 0人点赞 0条评论
Loading...

Split array column into rows

Use OPENJSON feature in sql server (2016+) to parse json array string into rows.

Let's first look this:

select * from openjson('["small.jpg","mid.jpg","big.jpg"]')
select * from openjson('{"A":"small1.jpg","B":"mid1.jpg","C":"big1.jpg"}')

Result:

key	value	     type
0	small.jpg	1
1	mid.jpg	        1
2	big.jpg	        1

key	value	     type
A	small1.jpg	1
B	mid1.jpg	1
C	big1.jpg	1

Now look at more complehensive example:

create table parts (id int, name varchar(255), images varchar(max))
insert into parts (id,name,images) values 
    (1, 'nail','["small.jpg","mid.jpg","big.jpg"]'),
    (2, 'screw','{"A":"small1.jpg","B":"mid1.jpg","C":"big1.jpg"}')

--split array column to rows
declare @tmp table(id int, img varchar(4000))
declare @id int,@img varchar(max)
declare csr cursor for
select id,images from parts
open csr
fetch next from csr into @id,@img
while @@fetch_status=0
begin
    insert into @tmp (id,img) select @id,value from openjson(@img)
    fetch next from csr into @id,@img
end
close csr
deallocate csr
select * from @tmp

Result:
Useful sql script for developers

Concat rows to a column

Using OPENXML feature in sql server to concat rows into a column. Reuse to script above:

--concat rows to a column
--using above example
select id, '_product_image_gallery', 
    stuff((select ','+img from @tmp where id=t.id for xml path('')),1,1,'')
from @tmp t
group by id

Result:
Useful sql script for developers

IT Team
Author: IT Team

Loading...
标签: Blog
最后更新:2020年10月20日

IT Team

stay absorbed stay excellent

点赞
< 上一篇
下一篇 >

文章评论

取消回复
最新 热点 随机
最新 热点 随机
Generating Test Data with SQL Scripts Remote connection to MySQL on SiteGround Hide author information from WordPress How to do some trick to improve WordPress performance Recovering user role in WordPress Sending email using gmail SMTP
How to do some trick to improve WordPress performanceRemote connection to MySQL on SiteGroundGenerating Test Data with SQL ScriptsRecovering user role in WordPressHide author information from WordPress
PHP - Delete folder Download video from zoom WordPress Featured Image in database Crawler - get data from unwrapped html content MySQL urlencode/urldecode that support chinese chars Fixing jQuery DataTables header non-responsive issue
Categories
  • AdSense
  • Architecture
  • BI
  • C#
  • CSS
  • Database
  • Digital Marketing
  • DotNET
  • Hosting
  • HTML
  • JavaScript
  • PHP
  • Program Language
  • Python
  • Security
  • SEO
  • Technology
  • Web
  • Wordpress

COPYRIGHT © 20203-2021 Hostlike.com. ALL RIGHTS RESERVED.