Skip to content Skip to sidebar Skip to footer

How Do I Cast A Bigint To A Varbinary To Compare Against A Timestamp?

I currently have a function which accepts a varbinary as it allows me to pass the timestamp for comparison against other timestamps without casting it however my comparisons are fa

Solution 1:

Hope this helps:

--It is no difference how you declare it
DECLARE @Test8 varbinary(8) = CAST(506693 as varbinary(8));
DECLARE @Test64 varbinary(64) = CAST(506693 as varbinary(64)); 
SELECT sys.fn_varbintohexstr(@Test8);  --0x0007bb45
SELECT sys.fn_varbintohexstr(@Test64); --0x0007bb45

--but it is a difference what you are casting
DECLARE @Test16 varbinary(16) = CAST(CAST(506693 AS bigint) as varbinary(16)); 
DECLARE @TestTS timestamp = CAST(506693 as Timestamp);
SELECT sys.fn_varbintohexstr(@Test16); --0x000000000007bb45
SELECT sys.fn_varbintohexstr(@TestTS)  --0x000000000007bb45

Post a Comment for "How Do I Cast A Bigint To A Varbinary To Compare Against A Timestamp?"