Select2 a.xm,xk=substring(a.xk,b.number,charindex(‘,‘,a.xk+‘,‘,b.number)-b.number) 3 from 4 表A a join master..spt_values b 5 ON b.type=‘p‘ AND b.number BETWEEN 1 AND LEN(a.xk)6 where7 substring(‘,‘+a.xk,b.number,1)=‘,‘
--1.将字符串转换为列显示 if object_id(‘tb‘) is not null drop table tb go create table tb([编号] varchar(3),[产品] varchar(2),[数量] int,[单价] int,[金额] int,[序列号] varchar(8)) insert into tb([编号],[产品],[数量],[单价],[金额],[序列号]) select ‘001‘,‘AA‘,3,5,15,‘12,13,14‘ union allselect ‘002‘,‘BB‘,8,9,13,‘22,23,24‘go select [编号],[产品],[数量],[单价],[金额] ,substring([序列号],b.number,charindex(‘,‘,[序列号]+‘,‘,b.number)-b.number) as [序列号] from tb a with(nolock),master..spt_values b with(nolock) where b.number>=1 and b.number<=len(a.[序列号]) and b.type=‘P‘and substring(‘,‘+[序列号],number,1)=‘,‘go drop table tb go /** 编号 产品 数量 单价 金额 序列号 ---- ---- ----------- ----------- ----------- -------- 001 AA 3 5 15 12 001 AA 3 5 15 13 001 AA 3 5 15 14 002 BB 8 9 13 22 002 BB 8 9 13 23 002 BB 8 9 13 24 */ ----------
--7.将字符串显示为行列 if object_id(‘tb‘) is not null drop table tb create table tb( id int identity(1,1), s nvarchar(100)) insert into tb(s) select ‘车位地址1,车位状况1|车位地址2,车位状况2|车位地址n,车位状况n‘;with cte as( select substring(s,number,charindex(‘|‘,s+‘|‘,number)-number) as ss from tb with(nolock),master..spt_values with(nolock) where type=‘P‘ and number>=1 and number<=len(s) and substring(‘|‘+s,number,1)=‘|‘)select left(ss,charindex(‘,‘,ss)-1)as s1,substring(ss,charindex(‘,‘,ss)+1,len(ss))as s2 from cte;drop table tb /** s1 s2 ----------- ------------ 车位地址1 车位状况1 车位地址2 车位状况2 车位地址n 车位状况n
MSSQL将逗号分隔的字符串转换成列显示
标签:
小编还为您整理了以下内容,可能对您也有帮助:
mssql,逗号分隔的字符串转换成一列数据
Select
a.xm,xk=substring(a.xk,b.number,charindex(',',a.xk+',',b.number)-b.number)
from
表A a join master..spt_values b
ON b.type='p' AND b.number BETWEEN 1 AND LEN(a.xk)
where
substring(','+a.xk,b.number,1)=','
mssql,逗号分隔的字符串转换成一列数据
Select
a.xm,xk=substring(a.xk,b.number,charindex(',',a.xk+',',b.number)-b.number)
from
表A a join master..spt_values b
ON b.type='p' AND b.number BETWEEN 1 AND LEN(a.xk)
where
substring(','+a.xk,b.number,1)=','
使用SQL如何把用逗号等字符隔开的字符串转换成列表
如何把用逗号等字符隔开的字符串转换成列表,下面依逗号分隔符为例:
比如有一个字符串,其值为:香港,张家港,北京,上海
用SQL把这个字符串转换成列表的方法是:
1、方法一
WITH A AS (SELECT '香港,张家港,北京,上海' A FROM DUAL)
SELECT DECODE(B,0,SUBSTR(A,C),SUBSTR(A,C,B-C)) city FROM
(
SELECT A,B,(LAG(B,1,0) OVER(ORDER BY LV))+1 C
FROM(
SELECT A,INSTR(A,',',1,LEVEL) B,LEVEL LV FROM A
CONNECT BY LEVEL <=(LENGTH(A) - LENGTH(REPLACE(A,',','')))+1
)
)
使用SQL如何把用逗号等字符隔开的字符串转换成列表
如何把用逗号等字符隔开的字符串转换成列表,下面依逗号分隔符为例:
比如有一个字符串,其值为:香港,张家港,北京,上海
用SQL把这个字符串转换成列表的方法是:
1、方法一
WITH A AS (SELECT '香港,张家港,北京,上海' A FROM DUAL)
SELECT DECODE(B,0,SUBSTR(A,C),SUBSTR(A,C,B-C)) city FROM
(
SELECT A,B,(LAG(B,1,0) OVER(ORDER BY LV))+1 C
FROM(
SELECT A,INSTR(A,',',1,LEVEL) B,LEVEL LV FROM A
CONNECT BY LEVEL <=(LENGTH(A) - LENGTH(REPLACE(A,',','')))+1
)
)
编写一个sql语句,把b列中的数据用逗号分隔开,变成很多?谢谢
这个我做过,拆分逗号分隔符变列
select a,
t.ca,
c.lv,instr(t.ca, ',', 1, c.lv) + 1,
substr(t.ca,
instr(t.ca, ',', 1, c.lv) + 1,
instr(t.ca, ',', 1, c.lv + 1) -
(instr(t.ca, ',', 1, c.lv) + 1)) AS c
from (select a,
',' || b || ',' AS ca,
b,
LENGTH(b),
length(b || ','),
REPLACE(b, ','),
length(REPLACE(b, ',')), --删除逗号的长度
nvl(length(REPLACE(b, ',')), 0), --空值长度为空,因此要将空值替换为0
length(b || ',') - nvl(length(REPLACE(b, ',')), 0) AS cnt --以逗号分隔的字符串个数,先在原来的长度上加1 计算去除所有逗号后字符串长度,再,拿这个
FROM TEST_A) t,
(select LEVEL lv from al CONNECT BY LEVEL <= 100) c
where c.lv <= t.cnt --逗号数量在100以内
SQL逗号分割一列数据的值,将结果变成一行多列
create table #t(ID int,Content varchar(4000))
insert into #t(ID,Content)
select 1,'22,5000,3000'
union all select 2,'1,35,200,2'
union all select 3,'802,22'
union all select 4,'213,354,2002,22,500'
select * from #t
declare @sql nvarchar(4000),@i int
set @i=1
while exists(select 1 from #t where Content<>'')
begin
set @sql='alter table #t add PKQ'+convert(varchar,@i)+' int'
exec(@sql)
set @sql='declare @loc int update #t set @loc=charindex('','',Content),PKQ'
+convert(varchar,@i)+'=convert(int,case @loc when 0 then Content else '
+'substring(Content,1,@loc-1) end),Content=case @loc when 0 then '''' else '
+'substring(Content,@loc+1,len(Content)-@loc) end where Content<>'''''
exec(@sql)
set @i=@i+1
end
select * from #t