這裡蒐索程式師資訊,查找有用的技術資料
当前位置:首页 » 编程语言 » 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