码迷,mamicode.com
首页 > 数据库 > 详细

SQL查出的字段一列分成两列

时间:2019-12-27 09:48:59      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:where   char   tab   lin   div   arc   color   str   int   

CREATE TABLE #tb(
lh VARCHAR(10))
INSERT INTO #tb SELECT 12-1-2202
union all SELECT 12-1-2301
union all SELECT 12-1-2302
union all SELECT 12-1-2401
union all SELECT 12-1-2402
union all SELECT 12-1-2501
union all SELECT 12-1-2502
union all SELECT 12-1-2601
union all SELECT 12-1-2602
union all SELECT 12-2-101
union all SELECT 12-2-102
union all SELECT 12-2-201
union all SELECT 12-2-202
union all SELECT 12-2-301
union all SELECT 12-2-302
union all SELECT 12-2-401
union all SELECT 12-2-402
SELECT lh,SUBSTRING(lh,4,1) as a INTO #s from #tb
select
   a.lh as ydy,b.lh as edy
from
(
select id=row_number()over(order by getdate()),lh from #s where a=1
)a
left join
(
select id=row_number()over(order by getdate()),lh from #s where a=2
)b
on
  a.id=b.id
DROP TABLE #tb
DROP table #s
----------------结果----------------------------
/* ydy         edy
----------- -----------
12-1-2202 12-2-101
12-1-2301 12-2-102
12-1-2302 12-2-201
12-1-2401 12-2-202
12-1-2402 12-2-301
12-1-2501 12-2-302
12-1-2502 12-2-401
12-1-2601 12-2-402
12-1-2602  null
(6 行受影响)
*/

 

SQL查出的字段一列分成两列

标签:where   char   tab   lin   div   arc   color   str   int   

原文地址:https://www.cnblogs.com/lydg/p/12105313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!