当前位置:首页 » 编程语言 » sql将翼型数据拆分成多行
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql将翼型数据拆分成多行

发布时间: 2023-05-16 16:59:27

sql数据库按符号拆分成多列和多行

你这样是把列2看成是一个多值数据了,用普通的sql是处理不了的。用plsql写个小过程来做很容易啊,把一行数据读出来,然后对列2进行逐个字符读取放到临时字符,遇到换行的就把列1和临时字符写到数据库,然后继续读

❷ 如何用SQL语句将数据一行变成多行呢

截断数据,在新增分隔符即可。
放大招
指定索引号,进行数据截取,返回处理后的字符串数组
string str =你的数据
str.substring(你想分的长度)你自己打印一个转换符就好了

❸ sql 语句怎么将一行拆分成两行

先创建spilt方法,然后调用就行。
1.
--创建function
create or replace function split
(
p_list varchar2,
p_sep varchar2 := ','
) return type_split pipelined
is
l_idx pls_integer;
v_list varchar2(50) := p_list;
begin
loop
l_idx := instr(v_list,p_sep);
if l_idx > 0 then
pipe row(substr(v_list,1,l_idx-1));
v_list := substr(v_list,l_idx+length(p_sep));
else
pipe row(v_list);
exit;
end if;
end loop;
return;
end split;

2.select * from table(split('曹xx,中XX'));

❹ sql 将多列数据转换成多行显示

select a as d from t1
union all
select b from t1
union all
select c from t1

通用只可用动态

declare @s nvarchar(4000)
select @s=isnull(@s+'union all select ','select ')+Name+' as D from 表名' from syscolumns where ID=object_id('表名')
exec(@s)

❺ mssql根据条件数据列分解成多行数据,急

创建测试表

createtablet
(idint,
namevarchar(10),
typevarchar(10))

insertintotvalues(1,'苹果','003')
insertintotvalues(2,'香蕉','A04')
insertintotvalues(3,'西瓜',NULL)
insertintotvalues(4,'石榴','002')

运行

selectt.*,t.name+s.name2name2fromtleftjoin
(selectdistincttype,
casewhenisnumeric(replace(type,0,''))=0then
left(replace(type,0,''),1)+cast(t1.idasvarchar)+'级'elsecast(t1.idasvarchar)+'级'endname2
fromt,
(select1inionall
select2inionall
select3inionall
select4id)t1
wheretypeisnotnull
andt1.id<=right(replace(type,0,''),1))s
ont.type=s.type

结果

selectdistincttype,
casewhenisnumeric(replace(type,0,''))=0then
left(replace(type,0,''),1)+cast(t1.idasvarchar)+'级'elsecast(t1.idasvarchar)+'级'endname2
fromt,
(select1inionall
select2inionall
select3inionall
select4id)t1
wheretypeisnotnull
andt1.id<=right(replace(type,0,''),1)

❻ SQL数据库 怎么将一行数据变成多行

用一个循环。
先用参数取到到底要变成多少行,和单价
declare
@t
int
declare
@sal
nvarchar(8)
根据需求或实际情况将应变为多少行这个烤取给变量@t,单价取给@sal
设置循环的起始数值
declare
@i
int
set
@i=1
开始循环
while(@i<=@t)
begin
--将单行的数据插入需要的表
insert
into(……)
values(……)
--循环参数自增
set
@i=@i+1
end

❼ SQL一行数据拆分成多行

select Name1 Spec1 Qty1 from A
union
select Name2 Spec2 Qty2 from A
union
select Name3 Spec3 Qty3 from A

❽ sql如何把行拆分为几行

如此问题,我也只能这样贴例子了,如不能解决,可以继续联系
--分拆列值
--原着:邹建
--改编:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)2007-12-16广东深圳

/*
有表tb,如下:
idvalue
----------------------
1aa,bb
2aaa,bbb,ccc
*/
--欲按id,分拆value列,分拆后结果如下:
/*
idvalue
-------------------
1aa
1bb
2aaa
2bbb
2ccc
*/
--1.旧的解决方法(sqlserver2000)
selecttop8000id=identity(int,1,1)into#fromsyscolumnsa,syscolumnsb

selectA.id,substring(A.[values],B.id,charindex(',',A.[values]+',',B.id)-B.id)
fromtbA,#B
wheresubstring(','+A.[values],B.id,1)=','

droptable#

--2.新的解决方法(sqlserver2005)

createtabletb(idint,valuevarchar(30))
insertintotbvalues(1,'aa,bb')
insertintotbvalues(2,'aaa,bbb,ccc')
go
selectA.id,B.value
from(
selectid,[value]=convert(xml,'<root><v>'+replace([value],',','</v><v>')+'</v></root>')fromtb
)A
outerapply(
selectvalue=N.v.value('.','varchar(100)')fromA.[value].nodes('/root/v')N(v)
)B

droptabletb

/*
idvalue
-----------------------------------------
1aa
1bb
2aaa
2bbb
2ccc

(5行受影响)
*/

❾ sql 一行数据改成多行

您的方法(不建视图):
SELECT TOP 100 PERCENT *
FROM (SELECT *, SUBSTRING(Picture, 32, 30) AS PICTUREV FROM dbo.procts
WHERE (SUBSTRING(Picture, 32, 30) <> '')
union
SELECT *, SUBSTRING(Picture, 63, 30) AS PICTUREV FROM dbo.procts
WHERE (SUBSTRING(PictureB, 63, 30) <> '')
union
SELECT *, SUBSTRING(Picture, 94, 30) AS PICTUREV FROM dbo.procts
WHERE (SUBSTRING(PictureB, 94, 30) <> '')
) DERIVEDTBL
ORDER BY ...

我觉得您的子串应该是这样计算的,:
SUBSTRING(Picture, 0*31+1,30)
SUBSTRING(Picture, 1*31+1,30)
SUBSTRING(Picture, 2*31+1,30)
SUBSTRING(Picture, 3*31+1,30)
所以,在语句中这样写也会更好写,更容易理解,和更不容易出错。也不影响效率。

你的方法除了多余建了那么多视图,应该说是不错的方法了,但是实现不了不等长文件名按分隔符分离字串。

如果是分隔符为字串分段,您就必须要用我的自定义函数加我的语句。
如果是您写的那样,是固定长度的字串分段,那么就完全用你的方法,便是稍作改动,不用建中间视图
另外,您喜欢视图的话,可以建一个视图:
create view DERIVEDTBL
as
create view
SELECT *, SUBSTRING(Picture, 0*31+1,30) AS PICTUREV FROM dbo.procts WHERE (SUBSTRING(Picture, 32, 30) <> '')
union
SELECT *, SUBSTRING(Picture, 1*31+1,30, 30) AS PICTUREV FROM dbo.procts WHERE (SUBSTRING(PictureB, 63, 30) <> '')
union
SELECT *, SUBSTRING(Picture, 2*31+1,30, 30) AS PICTUREV FROM dbo.procts WHERE (SUBSTRING(PictureB, 94, 30) <> '')
union
SELECT *, SUBSTRING(Picture, 3*31+1,30, 30) AS PICTUREV FROM dbo.procts WHERE (SUBSTRING(PictureB, 94, 30) <> '')
union
SELECT *, SUBSTRING(Picture, 4*31+1,30, 30) AS PICTUREV FROM dbo.procts WHERE (SUBSTRING(PictureB, 94, 30) <> '')
union
SELECT *, SUBSTRING(Picture, 5*31+1,30, 30) AS PICTUREV FROM dbo.procts WHERE (SUBSTRING(PictureB, 94, 30) <> '')
--....向下任意延伸,只要改SUBSTRING中的序号就行了。
go

调用时这样就行了:
SELECT TOP N *
FROM DERIVEDTBL
ORDER BY ...

=================
一句解决问题,这样:
SELECT ID,TITLE,DBO.MYSPLIT(PICTURES,1,'|') AS PICTURE,1 AS SN from TABLENAME
UNION
SELECT ID,TITLE,DBO.MYSPLIT(PICTURES,2,'|') AS PICTURE,2 AS SN from TABLENAME
UNION
SELECT ID,TITLE,DBO.MYSPLIT(PICTURES,3,'|') AS PICTURE,3 AS SN from TABLENAME
UNION
SELECT ID,TITLE,DBO.MYSPLIT(PICTURES,4,'|') AS PICTURE,4 AS SN from TABLENAME
UNION
SELECT ID,TITLE,DBO.MYSPLIT(PICTURES,5,'|') AS PICTURE,5 AS SN from TABLENAME
UNION
SELECT ID,TITLE,DBO.MYSPLIT(PICTURES,6,'|') AS PICTURE,6 AS SN from TABLENAME
-- . . . . . .
ORDER BY ID,TITLE,SN

还有,您的这个函数求等长字串,不知道为什么没有首字串?
SUBSTRING(Picture, 32, 30)
就是这个个?
SUBSTRING(Picture, 1, 30)

===================
这里只给您写了6行,来满足您每ID最多6张图片的要求。写多少行都可以,即每ID最大图片数为6张。您可以写100行,1000行都行!

这里还给您多加了以个序号字段(SN),以保证图片顺序与原排列相同。如果不需要您可以去掉他。

下面的函数是一个非常有用的自定义数,是用来提取字串中某一部份的,请您执行一下,把他建立到您的数据库中,今后对您的编程会提供很大的方便。当然,本例所用的函数就是这个函数。

create FUNCTION mysplit--将以某分隔符分段的字串,按指定的顺序号提取子串
(@str nvarchar(2000),--源字串
@sn int, --提取序号
@Deli varchar(1) --分隔符
)
RETURNS varchar(100)
AS
BEGIN
declare @first int,@last int,@result varchar(1000),@sn0 int
select @sn0=0,@first=0,@LAST=1,@str=@str+REPLICATE(@DELI,1)
while @sn0!=@sn
begin
select @sn0=@sn0+1,@first=@LAST,@last=charindex(@DELI,@str,@LAST)+1
end
if @last-@first-1<0
set @result=''
else
SET @RESULT=SUBSTRING(@str,@FIRST,@LAST-@FIRST-1)
RETURN ( @RESULT )
END
GO

❿ sql一行拆分多行记录

看个例子 改改再用 应该没问题
QL> select * from t00;

ID ACHAR
---------- ----------
1 ab,ac,ad
1 cc,cf
2 bb,bc,be

SQL> select * from (select id,regexp_substr(achar,'[^,]+',1,level) achar from t00 connect by level<=length(achar)-length(regexp_replace(achar,',',''))+1) group by id,achar order by id ;

ID ACHAR
---------- --------------------
1 ab
1 ac
1 ad
1 cc
1 cf
2 bb
2 bc
2 be

已选择8行。