风轻云淡

-------生活没有抱怨,只有自己的不努力,当才华撑不起梦想与野心,不断学习是你唯一的选择。没有伞的孩子必须努力奔跑...

导航

SqlServer时间格式不足2位补0

 

CREATE FUNCTION [dbo].[FULL_TIME](@Times NVARCHAR(50))
 RETURNS NVARCHAR(50)
 AS
 BEGIN
 DECLARE @return NVARCHAR(50)
 SET @return =(
  select case len(SUBSTRING(@Times,0,CHARINDEX(':', @Times, 0)))
 when 1
 then '0'+SUBSTRING(@Times,0,CHARINDEX(':',@Times, 0))
  else SUBSTRING(@Times,0,CHARINDEX(':',@Times, 0)) end
  +':'+case LEN(SUBSTRING(@Times,CHARINDEX(':', @Times, 0)+1,
 (CHARINDEX(':', @Times, CHARINDEX(':',@Times, 0)+1)-CHARINDEX(':',@Times, 0))-1) )
  when 1
  then '0'+SUBSTRING(@Times,CHARINDEX(':', @Times, 0)+1,
 (CHARINDEX(':', @Times, CHARINDEX(':',@Times, 0)+1)-CHARINDEX(':',@Times, 0))-1)
  else SUBSTRING(@Times,CHARINDEX(':', @Times, 0)+1,
 (CHARINDEX(':',@Times, CHARINDEX(':',@Times, 0)+1)-CHARINDEX(':',@Times, 0))-1)  end
 +':'+
 case LEN(SUBSTRING(@Times,CHARINDEX(':',@Times, CHARINDEX(':',@Times, 0)+1)+1,5))
 when 1
 then '0'+SUBSTRING(@Times,CHARINDEX(':',@Times, CHARINDEX(':',@Times, 0)+1)+1,5)
 else SUBSTRING(@Times,CHARINDEX(':', @Times, CHARINDEX(':', @Times, 0)+1)+1,5) end )
 RETURN @return
 END

 select dbo.FULL_TIME('3:1:2')  --结果:  03:01:02

posted on 2020-05-26 18:29  淡然→笑  阅读(1964)  评论(0编辑  收藏  举报