2015年06月12日
時刻の繰り上げ・繰り下げ(SQL2012以降)
過去記事の「時刻の繰り上げ・繰り下げ」の改訂版。
変更点:
・SQLServer2012から追加された「TimeFromParts」関数を使用した
・時刻を入れる引数のデータ型を「Time」型に変更
・上記により、返す値のレンジが00:00:00~23:59:59に変わる
・繰り上げの結果24時を越える場合は「時」を00時から計算する
ALTER FUNCTION dbo.scr_RoundTime2
(
@TimeValue time, @DivValue int, @sw int
)
RETURNS time
AS
BEGIN
declare @result time
--【エラー処理】時間単位が1~60の範囲でない場合、@TimeValueをそのまま返す
if @divvalue <= 0
begin
set @result = @TimeValue
return @result
end
if @divvalue >= 60
begin
set @result = @TimeValue
return @result
end
--引数swが3の場合、@TimeValueをそのまま返す
if @sw = 3
begin
set @result = @TimeValue
return @result
end
--「時」の部分を格納
declare @timepart_hour int
set @timepart_hour = datepart(hh, @TimeValue)
--「分」を時間単位で割った商と余り
declare @shou int
declare @amari int
set @shou = datepart(mi, @TimeValue) / @divvalue
set @amari = datepart(mi, @TimeValue) % @divvalue
--時間単位に掛ける数(商)
declare @takenum int
set @takenum = @shou
--余りが出た場合、時間単位に掛ける数を1増やす(sw=2・繰り下げの場合のみ)
if (@sw = 2 and @amari > 0)
begin
set @takenum = @takenum + 1
end
--「分」を計算する
declare @timepart_min int
set @timepart_min = @takenum * @divvalue
--「分」が60分を超えた場合、それを60で割って増えた分を「時」に加算する
if @timepart_min >= 60
begin
set @timepart_hour = @timepart_hour + (@timepart_min / 60)
set @timepart_min = @timepart_min % 60
end
--「時」が24時間を超えた場合、それを24で割って00時に加算する
if @timepart_hour >= 24
begin
--set @datepart_day = @datepart_day + (@timepart_hour / 24)
set @timepart_hour = @timepart_hour % 24
end
set @result = TIMEFROMPARTS(@timepart_hour, @timepart_min, 0, 0, 7)
-- Return the result of the function
RETURN @result
END
GO
変更点:
・SQLServer2012から追加された「TimeFromParts」関数を使用した
・時刻を入れる引数のデータ型を「Time」型に変更
・上記により、返す値のレンジが00:00:00~23:59:59に変わる
・繰り上げの結果24時を越える場合は「時」を00時から計算する
ALTER FUNCTION dbo.scr_RoundTime2
(
@TimeValue time, @DivValue int, @sw int
)
RETURNS time
AS
BEGIN
declare @result time
--【エラー処理】時間単位が1~60の範囲でない場合、@TimeValueをそのまま返す
if @divvalue <= 0
begin
set @result = @TimeValue
return @result
end
if @divvalue >= 60
begin
set @result = @TimeValue
return @result
end
--引数swが3の場合、@TimeValueをそのまま返す
if @sw = 3
begin
set @result = @TimeValue
return @result
end
--「時」の部分を格納
declare @timepart_hour int
set @timepart_hour = datepart(hh, @TimeValue)
--「分」を時間単位で割った商と余り
declare @shou int
declare @amari int
set @shou = datepart(mi, @TimeValue) / @divvalue
set @amari = datepart(mi, @TimeValue) % @divvalue
--時間単位に掛ける数(商)
declare @takenum int
set @takenum = @shou
--余りが出た場合、時間単位に掛ける数を1増やす(sw=2・繰り下げの場合のみ)
if (@sw = 2 and @amari > 0)
begin
set @takenum = @takenum + 1
end
--「分」を計算する
declare @timepart_min int
set @timepart_min = @takenum * @divvalue
--「分」が60分を超えた場合、それを60で割って増えた分を「時」に加算する
if @timepart_min >= 60
begin
set @timepart_hour = @timepart_hour + (@timepart_min / 60)
set @timepart_min = @timepart_min % 60
end
--「時」が24時間を超えた場合、それを24で割って00時に加算する
if @timepart_hour >= 24
begin
--set @datepart_day = @datepart_day + (@timepart_hour / 24)
set @timepart_hour = @timepart_hour % 24
end
set @result = TIMEFROMPARTS(@timepart_hour, @timepart_min, 0, 0, 7)
-- Return the result of the function
RETURN @result
END
GO
Posted by hr16de at 02:04│Comments(0)
│SQL Server
※このブログではブログの持ち主が承認した後、コメントが反映される設定です。