新学SQLSERVER请教如何分列

oXueYuZhiYing 2012-11-24 11:52:42
在表A中和“料号”列
SELECT 料号 FROM A
//----
料号
A001,102,499
A003,1023,39
A001-A,2.2,32
我想把料号这一列分成三列,中间用','做间隔
用SELECT 查询时表变成
品名 长度 宽度 面积
A001 102 499 102*499
A003 1023 39 1023*39
A001-A 2.2 32 2.3* 32
-----
请教各位要如何操作才能达到这个效果?
网上的例子很多,但是太复杂,实在看不懂
...全文
319 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
坚_持 2012-11-25
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
----------------------------------------------------------------
-- Author  :TravyLee(物是人非事事休,欲语泪先流!)
-- Date    :2012-11-24 12:45:57
-- Version:
--      Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
--	Jul  9 2008 14:43:34 
--	Copyright (c) 1988-2008 Microsoft Corporation
--	Developer Edition on Windows NT 6.1 <X86> (Build 7600: )
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null 
drop table [test]
go 
create table [test]
(
	[物料] varchar(13)
)
insert [test]
select 'A001,102,499' union all
select 'A003,1023,39' union all
select 'A001-A,2.2,32'
go
;with t
as(
select
	LEFT([物料],CHARINDEX(',',[物料])-1) as 品名,
	cast(SUBSTRING([物料],CHARINDEX(',',[物料])+1,CHARINDEX(',',[物料],CHARINDEX(',',[物料])+1)-CHARINDEX(',',[物料])-1) as  numeric(8,2))as 长度,
	cast(RIGHT([物料],CHARINDEX(',',REVERSE([物料]))-1) as numeric(8,2)) as 宽度
from
	test
)
select
	品名,
	长度,
	宽度,
	长度*宽度 as 面积
from
	t
/*
品名	长度	宽度	面积
-------------------------------------------------------
A001	102.00	499.00	50898.0000
A003	1023.00	39.00	39897.0000
A001-A	2.20	32.00	70.4000
*/
oXueYuZhiYing 2012-11-24
  • 打赏
  • 举报
回复
引用 2 楼 DBAXMMDS 的回复:
SQL code? 12345678910111213141516171819202122232425262728293031323334353637383940 --创建数据开始 if(object_id('a') is not null) drop table a go create table a ( material varchar(4), length decimal……
感谢你的回复,不过有点看不懂 可以做成一个自定函数,然后调用吗?
oXueYuZhiYing 2012-11-24
  • 打赏
  • 举报
回复
引用 1 楼 TravyLee 的回复:
subatring() charindex()函数
可以具体一点吗?
极品老土豆 2012-11-24
  • 打赏
  • 举报
回复


--创建数据开始
if(object_id('a') is not null) drop table a
go
create table a
(
material varchar(4),
length decimal(5,1),
width decimal(5,1)
)
go
insert into a
select 'A001',102,499 union all
select 'A003',1023,39 union all
select 'A001-A',2.2,32
go

--创建数据结束

--语句
select material as '品名',length as'长度',width as '宽度'
		,cast(length as varchar)+'*'+cast(width as varchar) as '面积'
		,width*length as '面积值'
from a


--结果展示
/*
品名   长度                                      宽度                                      面积                                                            面积值
---- --------------------------------------- --------------------------------------- ------------------------------------------------------------- ---------------------------------------
A001 102.0                                   499.0                                   102.0*499.0                                                   50898.00
A003 1023.0                                  39.0                                    1023.0*39.0                                                   39897.00
A001 2.2                                     32.0                                    2.2*32.0                                                      70.40

(3 行受影响)

*/


  • 打赏
  • 举报
回复
subatring() charindex()函数
极品老土豆 2012-11-24
  • 打赏
  • 举报
回复
这个是一个语句的事,用自定函数反而累赘。
引用 4 楼 oXueYuZhiYing 的回复:
引用 2 楼 DBAXMMDS 的回复: SQL code? 12345678910111213141516171819202122232425262728293031323334353637383940 --创建数据开始 if(object_id('a') is not null) drop table a go create table a ( material varc……

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧