格式转换问题

ffccdd6122 2014-10-20 08:07:30


请问专家,如何实现左表转换为右表,谢谢!
...全文
48 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaodongni 2014-10-20
  • 打赏
  • 举报
回复

with cte as 
(select '16-5' as lh,'02' as ylh union all
 select '16-12' as lh,'02' as ylh union all
 select '17-8' as lh,'09' as ylh union all
 select '11-02' as lh,'05' as ylh union all
 select '19' as lh,'01' as ylh union all
 select '221-3' as lh,'11' as ylh )
 select case when CHARINDEX('-',lh)=0  then lh 
  when LEN(lh)-CHARINDEX('-',lh)=1 then REPLACE(lh,'-','0')
 when LEN(lh)-CHARINDEX('-',lh)=2 then REPLACE(lh,'-','')
 end as lh,convert(int,ylh) as yhl from cte 

--结果
lh          yhl
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
1605       2
1612       2
1708       9
1102       5
19         1
22103     11

(6 行受影响)

还在加载中灬 2014-10-20
  • 打赏
  • 举报
回复
SELECT REPLACE(lh,'-','0')lh,CAST(ylh AS INT)ylh FROM 表名

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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