静·谧——Last Winner
===========================================================
===========================================================

SQL> select c2 from test;

C2
----------------------------------------
a,b,c,dd,efg
h,iii

select decode(lvl, 1, substr(c2, 1, instr(c2, ',', 1) - 1),
length(c2) - length(replace(c2, ',')) + 1, substr(c2, instr(c2, ',', 1, lvl-1)+1),
substr(c2, instr(c2, ',', 1, lvl-1) + 1, instr(c2, ',', 1, lvl) - instr(c2, ',', 1, lvl-1) -1))
what_you_need
from
( select level lvl
from (select data_length from user_tab_columns where table_name = 'TEST'
and column_name = 'C2')
connect by 1=1
and level <= data_length
),
test
where lvl < length(c2) - length(replace(c2, ',')) + 2
/

WHAT_YOU_NEED
-------------
a
b
c
dd
efg
h
iii

Written on 2005/03/13


稍微改善了一下,这回不用参照user_tab_columns

select
decode(lvl, 1, substr(c2, 1, instr(c2, ',', 1) - 1),
length(c2) - length(replace(c2, ',')) + 1, substr(c2, instr(c2, ',', 1, lvl-1)+1),
substr(c2, instr(c2, ',', 1, lvl-1) + 1, instr(c2, ',', 1, lvl) - instr(c2, ',', 1, lvl-1) -1))
what_you_need
from
(
select level lvl from (select max(length(c2) - length(replace(c2, ','))) + 1 maxlvl from test)
connect by 1 = 1 and level <= maxlvl
),
test
where lvl < length(c2) - length(replace(c2, ',')) + 2
/


俺狗尾续貂一个

select substr(c2,st+1,en-st-1) what_you_want from (select c2, decode(rn,1,0,instr(c2,',',1,rn-1)) st , decode(instr(c2,',',1,rn),0,length(c2)+1, instr(c2,',',1,rn)) en from (select distinct c2, level rn from test connect by level<=length(c2)-length(replace(c2,',',''))+1))
/

如果没有分隔符,比如'abc',hmxxyy的sql会取出null,偶的避免了这个问题:)

这个方法好啊,可以用来解决http://blog.itpub.net/post/7102/42711

lastwinner 发表于:2005.11.26 16:49 ::分类: ( Oracle , ) ::阅读:(2301次) :: 评论 (5)
re: Convert to rows by comma【By hmxxyy】 [回复]

请问SQL 2000中如何实现行列的转化?
另,我在论坛里询问机动车驾驶员管理软件的介绍和架构说明您有没有?谢谢

cup 评论于: 2005.12.02 11:45
re: Convert to rows by comma【By hmxxyy】 [回复]

我已经回复ITPUB里的那个帖子。

cup 评论于: 2005.12.02 11:46
re: Convert to rows by comma【By hmxxyy】 [回复]

谢谢回复。
行列转换和SQL里面的旋转数据表是不是一个概念?
前几天参加了金碟和海波龙共同开的管理绩效软件会议,对其中可以任意将行旋转变成列,列旋转变成行的功能印象非常深刻,简直和CORELDROW一样,呵呵,管理软件也可以这样做,确实震惊。这个不知道和你说的行列转换一样吗?这个功能实现起来复杂吗?

cup 评论于: 2005.12.09 21:37
re: Convert to rows by comma【By hmxxyy】 [回复]

transform
pivot table
就是把行数据转换到列上啊
select case a when a>b then c else0 end as q from table1

cup 评论于: 2005.12.10 13:49
re: Convert to rows by comma【By hmxxyy】 [回复]

打开MS SQL2000联机帮助,搜索"旋转数据表".上次的写错了,应该是:
select sum(case a when b>0 then c else 0 end)as f from tableA where .........

cup 评论于: 2005.12.15 17:04

发表评论
标题

在此添加评论
表情符号: smile laughing tongue angry crying sad wassat wink

称呼

邮箱地址(可选)

个人主页(可选)

 authimage


自我介绍
切换风格
新闻聚合
博客日历
文章归档...
最新发表...
最新评论...
最多阅读文章...
最多评论文章...
博客统计...
Blog信息
网站链接...