你這樣是把列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行。