Sample code for 30+ languages & platforms
SQL Server

Get MessageSet IDs as a Compact String

See more IMAP Examples

Demonstrates the Chilkat MessageSet.ToCompactString method, which returns the unique identifiers in ascending order using compact IMAP message-set syntax, collapsing consecutive runs into inclusive ranges. It takes no arguments. This example loads 1,2,3,4,5,8,9,10 and prints the compact form 1:5,8:10.

Background: The result is canonical for the current set — unique, sorted, and range-compacted — which is the compact notation IMAP servers expect for message-set arguments. It is the inverse of FromCompactString, so the two round-trip: a set emitted here can be reloaded later. The string contains only numbers and omits the HasUids setting, so track UID-vs-sequence separately.

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 @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the MessageSet.ToCompactString method, which returns the unique identifiers in
    --  ascending order using compact IMAP message-set syntax, with consecutive runs collapsed into
    --  inclusive ranges.  It takes no arguments.

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

    --  Load a set of individual identifiers.
    EXEC sp_OAMethod @msgSet, 'FromCompactString', @success OUT, '1,2,3,4,5,8,9,10'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @msgSet, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @msgSet
        RETURN
      END

    --  Get the canonical compact representation.
    DECLARE @compact nvarchar(4000)
    EXEC sp_OAMethod @msgSet, 'ToCompactString', @compact OUT

    PRINT @compact
    --  Output: 1:5,8:10

    EXEC @hr = sp_OADestroy @msgSet


END
GO