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

sql拆分列

發布時間: 2022-04-20 02:25:18

『壹』 sql語句拆分表中內容,並變成兩列

USEtempdb
GO
ALTERFUNCTIONDBO.SPLT
(@COLNVARCHAR(200),
@PRANVARCHAR(10)
)
RETURNSNVARCHAR(100)
AS

BEGIN
DECLARE@NUMNVARCHAR(100),@STRNVARCHAR(100),@IINT
SET@NUM=''
SET@STR=''
SET@I=1

WHILE(@I<=LEN(ISNULL(@COL,'')))
BEGIN
IFSUBSTRING(@COL,@I,1)IN('0','1','2','3','4','5','6','7','8','9')--說明是數字
SET@NUM=@NUM+SUBSTRING(@COL,@I,1)
ELSE
SET@STR=@STR+SUBSTRING(@COL,@I,1)
SET@I=@I+1

END
IF@PRA='STR'
SET@NUM=@STR
RETURN@NUM
END
GO
SELECTdbo.SPLT('A123,B23,BD21','STR'),dbo.SPLT('A123,B23,BD21','')

『貳』 在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 如何將一列分成若干列

這個..我試試吧..

先重建一個表吧,不過這次把這個列分為5列,然後我們要做的就是把你原來的數據倒過去

建表的語句就不寫了,這個表為tb_re,你原來的表為tb_or

insert into tb_re select **** from tb_or b;

****這里是關鍵,它包含了你要倒進tb_re的列的內容,當然了,select
之後的結果要和tb_re對應,難點應該就是怎麼拿那5個列出來

我假設你/分割的都只是個位數

現在:第一列 substr(b.aa,1,1)
2 substr(b.aa,3,1)
3 substr(b.aa,5,1)

4 substr(b.aa,7,1)
5 substr(b.aa,9,1)
不是個位數也行,不過就麻煩多了,其實如果樓主是在oracle中就容易做很多,在sql就難多了

感覺是數據表設計問題,以後不要這樣了...

『肆』 sql查詢中,如何將某列 分成 兩列。

SELECT PAccM33g02,

CASE PAccM33g02

WHEN 0 THEN PAccM33g02 END PAccM33g02_J,

CASE PAccM33g02

WHEN 1 THEN PAccM33g02 END PAccM33g02_C

FROM PAccM3307

『伍』 sql語句 拆分

select 這個地方就是查詢的欄位,然後IP是自定義的名字 'IP'=substring(拆分的欄位名,b.number,charindex('拆分的符號',拆分的欄位名+',',b.number)-b.number) from 你的表名 a
inner join master.dbo.spt_values b on b.number between 1 and len(拆分的欄位名)
and substring(','+拆分的欄位名,b.number,1)='拆分的符號'
where b.type='P' 後邊可以是條件 and Id=1

『陸』 sql中將一列按照不同長度分成幾列

用CASE WHEN來判斷,分解:

selectCASEWHENLen(a.menu_code)=2THENa.menu_codeELSE''ENDASMenu1
,CASEWHENLen(a.menu_code)=4THENa.menu_codeELSE''ENDASMenu2
,CASEWHENLen(a.menu_code)=6THENa.menu_codeELSE''ENDASMenu3
,a.menu_name
,c.role_name,e.user_in_station_mc,e.user_name,e.bm
frommenu_nameasa
leftjoinmenu_userasbonb.menu_code=a.menu_code
leftjoinuser_roleasconc.role_code=b.user_code
leftjoinuser_role_dyasdond.role_code=c.role_code
leftjoinuser_rightaseone.user_code=d.user_code
wheree.user_state='正常'
anda.menu_namelike'%交款%'

『柒』 怎樣將SQL查出的欄位分成兩列解決辦法

select
substr(欄位名稱,1,x),substr(欄位名稱,x+1)
from
表名;
其中X代表你要截取的該欄位要設置為第一列的長度。
例如:
select
substr(empno,1,2)第一列,substr(empno,3)第二列
from
emp;
這樣查詢出來顯示的就把
empno
欄位分為了2列。

『捌』 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