Friday, February 24, 2012

Convert Long To DateTime

Hi.

I need to convert a vb function into a user-defined function in a stored procedure.
The vb function converts a long datatype into Date datatype.

Here is the VB function:

Function LongToTime(lTime As Long) As Date

LongToTime = (lTime \ 10000) / 24 + ((lTime Mod 10000) \ 100) / 1440 + (lTime Mod 100) / 86400

End Function

The function is used to convert a timestamp(hhmmss) into a more readable format.
Would it be possible to create a function similar to this in an SQL stored procedure?

Thanks in advance
Wesley

translation of your vb function should be

CREATE FUNCTION fn_LongToTime (@.lTime int)

RETURNS smalldatetime

AS

BEGIN

DECLARE @.dt smalldatetime

Select @.dt = convert(smalldatetime, floor(@.ts / 10000) / 24 + Floor((@.ts % 10000) / 100) / 1440) + (@.ts % 100) / 86400)

RETURN @.Kg

END

GO

You have to verify smalldatetime conversion is right...

No comments:

Post a Comment