SQL Server 2025: BASE64_ENCODE() and BASE64_DECODE()**
Introduction
SQL Server has always required custom functions, CLR code, or external tools to handle Base64 encoding. That changes with SQL Server 2025, which introduces two native functions:
• BASE64_ENCODE()
• BASE64_DECODE()
These simplify secure data transport, token handling, API payload encoding, and binary-to-text conversion. They are useful across authentication systems, REST API integration, security workflows, and data masking patterns.
Today, we’ll look at what these functions do, how they work, key use cases, performance considerations, and how they tie directly into the REST endpoint invocation feature introduced on Day 10.
What BASE64_ENCODE() Does
BASE64_ENCODE converts VARBINARY data into a Base64 text string.
Example:
SELECT BASE64_ENCODE(CONVERT(VARBINARY(200), 'Valhallan2025'))
AS EncodedValue;
Sample output:
VmFsaGFsbGFuMjAyNQ==
This is useful for:
• Preparing payloads for APIs
• Securing tokens
• Storing signatures
• Encoding binary blobs for transport
• Building authorization headers
What BASE64_DECODE() Does
BASE64_DECODE converts Base64 text back into raw binary.
SELECT CONVERT(VARCHAR(MAX),
BASE64_DECODE('VmFsaGFsbGFuMjAyNQ=='))
AS DecodedValue;
Output:
Valhallan2025
This solves a long-standing limitation in SQL Server around binary and token manipulation.
Supported Scenarios
The Base64 functions work on:
• VARBINARY
• NVARCHAR
• VARCHAR
• UTF-8 encoded data
• JSON payloads
• Tokens in REST calls
They integrate cleanly with:
• External REST endpoint invocation
• JSON data type
• JSON indexes
• Encryption functions
• FILESTREAM / VARBINARY(MAX) columns
Examples You’ll Use in Real Systems
1. Converting user secrets for API calls
DECLARE @key VARBINARY(MAX) = CONVERT(VARBINARY(200),'SuperSecretKey');
SELECT BASE64_ENCODE(@key);
Used for:
• HTTP basic authentication
• OAuth2 signing
• Custom token generation
2. Embedding Base64 in JSON outpu
SELECT JSON_OBJECT(
'player', 'Luke',
'signature', BASE64_ENCODE(0x12345678)
);
3. Decoding inbound API payloads
SELECT BASE64_DECODE(JSON_VALUE(@payload,'$.encoded'))
AS RawBinary;
4. Handling images or binary files
SELECT BASE64_ENCODE(Avatar)
FROM PlayerAvatars;
This can transform blobs into a web-ready string for transport.
5. Secure hashing workflows
Even though Base64 isn’t encryption, it’s commonly used to transport encrypted values:
SELECT BASE64_ENCODE(HASHBYTES('SHA2_256', 'Valhallan'));
BASE64 in REST Endpoint Invocation
This becomes extremely important in Day 10.
Many REST APIs expect:
• Authorization headers
• Base64 credentials
• Signed JWT segments
• Binary model outputs encoded as Base64
Example inside sp_invoke_external_rest_endpoint:
EXEC sys.sp_invoke_external_rest_endpoint
@method = 'POST',
@url = 'https://api.example.com/upload',
@headers = '{"Authorization": "Basic '+
BASE64_ENCODE(CONVERT(VARBINARY(200),'user:password')) +'"}',
@payload = '{"data":"' + BASE64_ENCODE(@binaryDocument) + '"}';
SQL Server 2025 can now handle these scenarios without external scripts.
Performance Considerations
BASE64_ENCODE and DECODE are fast.
They run as native engine functions and avoid:
• CLR overhead
• Loops
• Custom functions
• Conversion hacks
On large VARBINARY(MAX) values, performance depends mainly on I/O, not CPU.
Common Mistakes
1. Forgetting to convert binary back to text
SELECT BASE64_DECODE('VmFsbGE=');
Returns VARBINARY.
You must convert:
SELECT CONVERT(VARCHAR(MAX),
BASE64_DECODE('VmFsbGE='));
2. Double-encoding
Be careful not to encode already Base64 text.
3. Using it as encryption
Base64 is encoding, not security.
It hides values, but does not protect them.
Workshop: BASE64 Hands-On
Step 1. Encode text
SELECT BASE64_ENCODE(CONVERT(VARBINARY(MAX), 'Hello SQL 2025'))
AS Encoded;
Step 2. Decode it
SELECT CONVERT(VARCHAR(MAX),
BASE64_DECODE(
BASE64_ENCODE(CONVERT(VARBINARY(MAX),'Hello SQL 2025'))
)) AS RoundTrip;
Step 3. Encode a binary file
Simulate binary data:
DECLARE @bin VARBINARY(MAX) = 0xDEADBEEF010203;
SELECT BASE64_ENCODE(@bin);
Step 4. Include Base64 in an API payload
DECLARE @json JSON =
'{"data":"' +
BASE64_ENCODE(CONVERT(VARBINARY(MAX),'UploadThis'))
+ '"}';
SELECT @json;
Step 5. Decode JSON value
SELECT CONVERT(VARCHAR(MAX),
BASE64_DECODE(JSON_VALUE(@json,'$.data')));
Final Thoughts
These Base64 functions seem minor on paper, but they enable entire classes of modern development that SQL Server previously handled poorly. They simplify REST integrations, secure token workflows, binary transport, and interop with cloud systems. Combined with JSON, REST invocation, and RegEx support, SQL Server 2025 becomes far more cloud-ready and API-friendly.
Tomorrow, Day 6 introduces the first RegEx function: REGEXP_LIKE(), finally giving SQL Server a built-in, engine-level regular expression evaluator.
References
• BASE64 Functions
https://learn.microsoft.com/sql/t-sql/functions/base64-encode-decode
• Binary and Encoding Functions
https://learn.microsoft.com/sql/t-sql/functions/binary-string-functions
• SQL Server 2025 New Features
https://learn.microsoft.com/sql/sql-server/what-s-new-in-sql-server-2025
Discover more from SQLYARD
Subscribe to get the latest posts sent to your email.


