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