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


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 行受影響)
*/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM