當前位置:首頁 » 編程語言 » sql切分數據成多列
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql切分數據成多列

發布時間: 2022-08-15 04:39:30

sql 怎麼將一列中的數據按一個分隔符分成多列顯示

withHrmResource(id,lastname)as(
select167,'段秋月'unionall
select170,'楊子軍'
)
,formtable_main_78(Requestid,WTCW,XMCY)as(
select722,'xxxxx公司','167,170'
)

selecta.*,stuff(c.[col],1,1,'')[項目名稱]
fromformtable_main_78a
crossapply
(select','+lastnamefromHrmResourceb
wherecharindex(','+convert(varchar,b.id)+',',','+a.XMCY+',')>0
forxmlpath(''))c([col])

結果:

❷ sql 怎麼總金額合計拆分成多列 比如合計由三個組成,『「100」,「200」,「300」,這

你這個沒辦法拆分的,首先,按照你的說法,這部分是沒有規律的。假如說我拆成200,200,200也行啊。是不是,沒有一定的規律怎麼拆分。
如果你原來顯示的是100,200,300,,那你計算完sum後,在後面在寫上這些欄位就好了,如果有什麼具體要求,還是要寫詳細的,現在你的這種說法只能說,不可能,因為拆的方式(100,200,300)(200,200,200)太多了,怎麼拆啊。

❸ sql資料庫按符號拆分成多列和多行

你這樣是把列2看成是一個多值數據了,用普通的sql是處理不了的。用plsql寫個小過程來做很容易啊,把一行數據讀出來,然後對列2進行逐個字元讀取放到臨時字元,遇到換行的就把列1和臨時字元寫到資料庫,然後繼續讀

❹ SQLsever management 一列拆分為多列

假設原來的列名為column1
Select
Substring(Column1,1,2) as Num1,
Substring(Column1,4,2) as Num2,
Substring(Column1,7,2) as Num3,
Substring(Column1,10,2) as Num4,
Substring(Column1,13,2) as Num5,
Substring(Column1,16,2) as Num6,
Substring(Column1,19,2) as Num7
From
table

❺ 在SQL中如何把一列字元串拆分為多列,請高手

--首先,你是按什麼規則拆? 我舉個例子 你要按欄位中的逗號拆開,假設欄位名叫text--用charindex和substring這2個函數 select substring(text,1,charindex(',',text)-1) as [before], substring(text,charindex(',',text),len(text)) as [after] from table

❻ SQL逗號分割一列數據的值,將結果變成一行多列

createtable#t(IDint,Contentvarchar(4000))
insertinto#t(ID,Content)
select1,'22,5000,3000'
unionallselect2,'1,35,200,2'
unionallselect3,'802,22'
unionallselect4,'213,354,2002,22,500'
select*from#t

declare@sqlnvarchar(4000),@iint
set@i=1
whileexists(select1from#twhereContent<>'')
begin
set@sql='altertable#taddPKQ'+convert(varchar,@i)+'int'
exec(@sql)
set@sql='declare@locintupdate#tset@loc=charindex('','',Content),PKQ'
+convert(varchar,@i)+'=convert(int,case@locwhen0thenContentelse'
+'substring(Content,1,@loc-1)end),Content=case@locwhen0then''''else'
+'substring(Content,@loc+1,len(Content)-@loc)endwhereContent<>'''''
exec(@sql)
set@i=@i+1
end
select*from#t

❼ mysql中將一列以逗號分隔的值分割成多列顯示

可以用
SUBSTRING
_INDEX()函數
在mysql中提供了一些
字元串操作
的函數,其中SUBSTRING_INDEX(str,
delim,
count)
str:
要處理的字元串
delim:
分割符
count:
計數
如果為正數,則從左開始數,如果為負數,則從右開始數

❽ sql 一列如何按條件分成多列

------------------------------------------------------------------------
-- author:jc_liumangtu(【DBA】小七)
-- date: 2010-03-26 09:37:30
-- version:
-- Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
-- Oct 14 2005 00:33:37
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
------------------------------------------------------------------------use test
set nocount on
if object_id('test4','U') is not null
drop table test4
go
create table test4
(item int ,date char(8),type char(2),quty int)
insert into test4
select 1000 , '20100101', 'A' , 100 union all
select 2000 , '20100101' ,'B' , 200 union all
select 1000 , '20100101' ,'C' , 100 union all
select 2000 , '20100101' , 'D' , 100
select * from test4select item ,date
,max(case type when 'A' then quty else 0 end) [typeA]
,max(case type when 'B' then quty else 0 end) [typeB]
,max(case type when 'C' then quty else 0 end) [typeC]
,max(case type when 'D' then quty else 0 end) [typeD]
from test4 group by item,date
item date type quty
----------- -------- ---- -----------
1000 20100101 A 100
2000 20100101 B 200
1000 20100101 C 100
2000 20100101 D 100item date typeA typeB typeC typeD
----------- -------- ----------- ----------- ----------- -----------
1000 20100101 100 0 100 0
2000 20100101 0 200 0 100

❾ 在SQL中怎麼把一列字元串拆分為多列,請高手賜教

--首先,你是按什麼規則拆?我舉個例子你要按欄位中的逗號拆開,假設欄位名叫text
--用charindex和substring這2個函數
selectsubstring(text,1,charindex(',',text)-1)as[before],substring(text,charindex(',',text),len(text))as[after]fromtable

❿ 在sql中如何把一列的值拆分成多列,求高手指教

其實可以理解為,多列取同一個欄位的值。