当前位置: 代码迷 >> Sql Server >> 格式转换,该怎么解决
  详细解决方案

格式转换,该怎么解决

热度:60   发布时间:2016-04-24 09:51:53.0
格式转换


不好意思,账号没分了,用另一账号另开一贴。

如何实现左表转换为右表,谢谢专家!
------解决思路----------------------
SELECT REPLACE(lh,'-','0')lh,CAST(ylh AS INT)ylh FROM 表名

你试下~~
------解决思路----------------------


declare @tab table(lh varchar(30),ylh varchar(30))
insert into @tab
select '12','3' union all
select '16-5','02' union all
select '16-12','03' union all
select '1201','7' union all
select '17-8','09' union all
select '11-02','05' union all
select '19','01' union all
select '221-3','11'

select case when charindex('-',lh)>0 then substring(lh,1,charindex('-',lh)-1)+right('00'+substring(lh,charindex('-',lh)+1,len(lh)),2) 
else lh end as lh,cast(ylh as int) as ylh
from @tab

------解决思路----------------------





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 行受影响)



这不是回复过了吗, 我刷了半天。就说已经结贴了这么又来了。
  相关解决方案