Sample code for 30+ languages & platforms
SQL Server

Demonstrate the Global.AutoQBDecode property

The Global.AutoQBDecode property can be set to _TRUE_ to cause Q/B encoded string arguments passed to any Chilkat method to be automatically decoded before being used.

Note: This example requires Chilkat v10.0.0 or later.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- Turn on auto Q/B decoding for all strings passed to any Chilkat method in any Chilkat class.
    DECLARE @glob int
    EXEC @hr = sp_OACreate 'Chilkat.Global', @glob OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @glob, 'AutoQBDecode', 1

    -- ---------------------------------------------------------------------------------------------------------------------------------------------
    -- "Q" Encoding

    -- Quoted-Printable encoding is primarily used for text that is mostly ASCII with some non-ASCII characters. 
    -- It encodes these characters in a way that remains mostly readable and compatible with ASCII-only systems.

    -- How It Works with UTF-8:
    -- - ASCII characters (except special characters like `=`) are encoded as themselves.
    -- - Non-ASCII characters are represented by their UTF-8 byte values, each byte encoded as `=` followed by two hexadecimal digits. 
    --   For example, the UTF-8 character "é" (U+00E9) is encoded as `=C3=A9`.

    -- For example, consider the text "Café" encoded in UTF-8. The UTF-8 bytes for "é" are `C3 A9`, so in Quoted-Printable, it looks like this:  Caf=C3=A9

    -- The "Q" encoding has this syntax:  "=?charset?q?encoded_text?="
    -- For example: "=?UTF-8?Q?Caf=C3=A9_announcement?="

    -- ------
    -- "B" Encoding

    -- Base64 encoding is used to encode non-ASCII text, making it more suitable for text with a high density of non-ASCII characters, such as those found in non-Western European languages.

    -- For example, consider the text "こんにちは" ("Hello" in Japanese).
    -- The "B" encoded string would be "=?UTF-8?B?44GT44KT44Gr44Gh44Gv?="

    -- ------
    -- Q encoding is suitable for text that is mostly ASCII.
    -- B Encoding is best for text that is densely packed with non-us-ascii chars, such as non-Latin (Asian) languages.
    -- ---------------------------------------------------------------------------------------------------------------------------------------------

    -- You can use Chilkat's online tool at Online Binary Encoder to pre-encode your literal strings before
    -- inserting them into your source code.

    -- When using the online tool, choose either "MIME header Q Encoding" or "MIME Header B Encoding".

    -- For example:
    DECLARE @s_cafe nvarchar(4000)
    SELECT @s_cafe = '=?utf-8?Q?Caf=C3=A9?='
    DECLARE @s1 int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @s1 OUT

    DECLARE @success int
    EXEC sp_OAMethod @s1, 'Append', @success OUT, @s_cafe
    EXEC sp_OAMethod @s1, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0
    -- Output is Café

    DECLARE @s_hello nvarchar(4000)
    SELECT @s_hello = '=?UTF-8?B?44GT44KT44Gr44Gh44Gv?='
    DECLARE @s2 int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @s2 OUT

    EXEC sp_OAMethod @s2, 'Append', @success OUT, @s_hello
    EXEC sp_OAMethod @s2, 'GetAsString', @sTmp0 OUT
    PRINT @sTmp0
    -- Output is こんにちは

    EXEC @hr = sp_OADestroy @glob
    EXEC @hr = sp_OADestroy @s1
    EXEC @hr = sp_OADestroy @s2


END
GO