Sample code for 30+ languages & platforms
SQL Server

Extract Timestamp from ULID

See more ULID/UUID Examples

Extract the date/time from a ULID.

Important: Chilkat's ULID functionality was introduced in v9.5.0.94.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @ulid nvarchar(4000)
    SELECT @ulid = '01GRH14AA82EY9A7S99YYF2QDY'

    DECLARE @dt int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Unix timestamps stored in ULIDs should be UTC...
    DECLARE @bLocal int
    SELECT @bLocal = 0
    -- Set the CkDateTime from the timestamp contained in the ULID
    EXEC sp_OAMethod @dt, 'SetFromUlid', @success OUT, @bLocal, @ulid
    IF @success = 0
      BEGIN

        PRINT 'ULID was not valid.'
        EXEC @hr = sp_OADestroy @dt
        RETURN
      END

    -- You can now get the date/time in any desired format.
    -- For example:

    EXEC sp_OAMethod @dt, 'GetAsUnixTime', @iTmp0 OUT, @bLocal
    PRINT 'Unix timestamp = ' + @iTmp0

    EXEC sp_OAMethod @dt, 'GetAsRfc822', @sTmp0 OUT, @bLocal
    PRINT 'RFC822 = ' + @sTmp0

    EXEC sp_OAMethod @dt, 'GetAsTimestamp', @sTmp0 OUT, @bLocal
    PRINT 'Timestamp = ' + @sTmp0

    -- Sample output:

    -- Unix timestamp = 1675608861
    -- RFC822 = Sun, 05 Feb 2023 14:54:21 GMT
    -- Timestamp = 2023-02-05T14:54:21Z

    EXEC @hr = sp_OADestroy @dt


END
GO