
Originariamente inviata da
nman
Certamente si potra fare molto meglio,
ma io con SQLServer 2008 R2 sono riuscito ad arrivare solo a quanto sotto .......
Formato Nvarchar oppure DateTime
per il formato Time con la mia limitatezza non riesco
Ciao,
hai fatto tutto
... basta fare il cast a time(0) invece che a datetime
select cast('00:09:00' as time(0))
Casomai , per rendere la query più leggibile si potrebbe creare una function;
.... tipo questa giusto per fare anche un esempio di function in cui si usano i costrutti elementari
di programmazione messi a disposizione, ma andrebbe bene anche come hai suggerito tu
codice:
use tempdb
go
if object_id('dbo.CvIntToTime') is not null
drop function dbo.CvIntToTime
go
create function dbo.CvIntToTime(@ihms int)
returns time(0)
as
begin
if @ihms is null return null
declare @shms varchar(6);
set @shms = CAST(@ihms as varchar(6))
declare @tr time(0);
declare @r int;
declare @s varchar(6)
declare @mul int
set @tr='00:00:00'
set @r=0
set @mul=1
while (LEN(@shms)>0)
begin
set @s=right(@shms,2);
if @mul=1
set @r= @r + cast(@s as int)
else if @mul=2
set @r= @r + cast(@s as int) * 60
else
set @r= @r + cast(@s as int) * 3600
set @shms=left(@shms,LEN(@shms)-len(@s));
set @mul=@mul+1
end;
return dateadd(second, @r, @tr);
end
go
select dbo.CvIntToTime(900);

P.S.
oltre 235959 'scavalla'