首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎样将SQL查出的字段分成两列啊解决办法

2012-02-11 
怎样将SQL查出的字段分成两列啊1201211120114860120000023000000200000112315671200539123156820000032000

怎样将SQL查出的字段分成两列啊
1201211
1201148
601
2000002
3000000
2000001
1231567
1200539
1231568
2000003
2000004
2000005
1231569
2000006
NULL
3000010
1231570
1231571
1231572
1231573
1231574
2000012
3000002
3000004
3000003
NULL
2000016
3000005
2000013
NULL
2000017
3000007
3000006
2000014
2000015
NULL
1231575
3000008
1231576
NULL
1231577
2000018
3000009
1231578
2000019
3000011
0
2000020
0
0
0
0
0

我要把以1开头和以2开头的数据查出来并分别形成一列 SELECT [ID]
  
  ,[APCode]
  
  FROM [APProtal].[dbo].[RegisterInfo]

[解决办法]

探讨
消息 207,级别 16,状态 1,第 4 行
列名 'colname' 无效。
消息 207,级别 16,状态 1,第 1 行
列名 'colname' 无效。
消息 207,级别 16,状态 1,第 2 行
列名 'colname' 无效。

[解决办法]
SQL code
------------------------------ Author  :fredrickhu(小F,向高手学习)-- Date    :2011-12-28 10:56:31-- Version:--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) --    Apr 22 2011 11:57:00 --    Copyright (c) Microsoft Corporation--    Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)--------------------------------> 测试数据:[tb]if object_id('[tb]') is not null drop table [tb]go create table [tb]([col] int)insert [tb]select 1201211 union allselect 1201148 union allselect 601 union allselect 2000002 union allselect 3000000 union allselect 2000001 union allselect 1231567 union allselect 1200539 union allselect 1231568 union allselect 2000003 union allselect 2000004 union allselect 2000005 union allselect 1231569 union allselect 2000006--------------开始查询--------------------------select   a.col,b.colfrom(select id=row_number()over(order by getdate()),* from tb where left(col,1)=1)aleft join(select id=row_number()over(order by getdate()),* from tb where left(col,1)=2)bon  a.id=b.id----------------结果----------------------------/* col         col----------- -----------1201211     20000021201148     20000011231567     20000031200539     20000041231568     20000051231569     2000006(6 行受影响)*/
[解决办法]
SQL code
select case when colname like'1%' then colname end as 列1,                 case when colname like'2%' then colname end as 列2          from table 

热点排行
Bad Request.