วันเสาร์ที่ 30 สิงหาคม พ.ศ. 2557

SQL Server Query : Convert int value into hour string format.

USE [test]
GO
/****** Object:  UserDefinedFunction [dbo].[func_convertint_to_hourstring]    Script Date: 08/31/2014 04:01:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[func_convertint_to_hourstring]
(
-- Add the parameters for the function here
@minutesint int
)
RETURNS nvarchar(5)
AS
BEGIN
declare @hourpart nvarchar(2);
declare @minutepart nvarchar(2);
declare @show nvarchar(5);

-- convert int to hour string
set @hourpart = @minutesint / 60;
set @minutepart = @minutesint % 60;
if(LEN(@minutepart) < 2)
begin
set @minutepart = '0' + @minutepart;
end;
set @show = @hourpart + ':' + @minutepart;

-- Return the result of the function
RETURN @show;
END
go

declare @ret nvarchar(5);
exec @ret = [func_convertint_to_hourstring] 390;
select @ret;

-- output = 6:30

ไม่มีความคิดเห็น:

แสดงความคิดเห็น