I have the following hexadecimal data that I want to convert using the postgres functions, the data they receive is the following:
@AVLData_TimeStamp @AVLData_GPSElement_Altitude
00000174C0FA7EA0 0140
And I want to convert them to the following data
@MOPO_FECHAHORA @MOPO_ALTITUD
2020-09-24 09:37:56.000 320
I have tried to do it using this function but have not succeeded
SELECT to_timestamp(@AVLData_TimeStamp)
I have two functions in SQLserver that do this but in postgres I don't know how to do it
First Function:
CREATE FUNCTION FUN_Unix_To_Date(
@FechaUnix CHAR(38)
)
RETURNS DATETIME
AS
BEGIN
DECLARE
@LocalTime BIGINT
,@Adjusted BIGINT
,@seconds BIGINT = @FechaUnix
SET @LocalTime = DATEDIFF(MS,GETDATE(),GETUTCDATE())
SET @Adjusted = @seconds - @LocalTime
RETURN (DATEADD(SECOND,CONVERT(BIGINT, LEFT(CAST(@Adjusted AS VARCHAR(100)),13))/1000, CAST('1970-01-01 00:00:00' AS DATETIME)))
END
Two Function:
CREATE FUNCTION HexadecimalToDec_v2(
@hexval CHAR(38)
)
RETURNS NUMERIC(38)
AS
BEGIN
DECLARE @i INT
,@digits INT
,@result NUMERIC
,@current_digit CHAR(1)
,@current_digit_dec NUMERIC
SET @digits = LEN(@hexval)
SET @i = 0
SET @result =0
WHILE @i <= @digits
BEGIN
SET @current_digit = SUBSTRING(@hexval, @i, 1)
IF @current_digit = 'A' OR
@current_digit = 'B' OR
@current_digit = 'C' OR
@current_digit = 'D' OR
@current_digit = 'E' OR
@current_digit = 'F'
SET @current_digit_dec = ASCII(@current_digit) - ASCII('A') + 10
ELSE
SET @current_digit_dec = CONVERT(INT,@current_digit)
SET @result = (@result * 16) + @current_digit_dec
SET @i = @i + 1
END
RETURN(@result)
END
and finally the function is used like this
@MOPO_FECHAHORA = (dbo.Unix_To_Date(dbo.FUN_HexadecimalToDec_v2(@AVLData_TimeStamp)))
And this is the result
@MOPO_FECHAHORA
2020-09-24 09:37:56.000
Thank you very much for your help.