Skip to main content
Solved

how do i convert a picture from filesystem into Base64 using SQL


Forum|alt.badge.img+7

I am creating an api link and need to upload an image as BASE64 code to add it in the GraphQl string. However, I only have the reference to the disk of the image in the database. How can I convert such an image to BASE64 code using TSQL?

Best answer by Anne Buit

Hi Marco,

You can use a trick with XML to turn binary data into base64 in T-SQL:

declare @binary_data varbinary(max) = 0x0102030405060708090A0B0C0D0E0F

-- Turn into base64
select cast('' as xml).value('xs:base64Binary(sql:variable("@binary_data"))', 'varchar(max)')

To obtain the binary data from the file, I’d suggest using a Read file connector.

View original
Did this topic help you find an answer to your question?
This topic has been closed for replies.

2 replies

Anne Buit
Community Manager
Forum|alt.badge.img+5
  • Community Manager
  • 653 replies
  • Answer
  • February 9, 2024

Hi Marco,

You can use a trick with XML to turn binary data into base64 in T-SQL:

declare @binary_data varbinary(max) = 0x0102030405060708090A0B0C0D0E0F

-- Turn into base64
select cast('' as xml).value('xs:base64Binary(sql:variable("@binary_data"))', 'varchar(max)')

To obtain the binary data from the file, I’d suggest using a Read file connector.


Forum|alt.badge.img+7

Hi Anne,

 

Thanks a lot. This is now working just fine. You saved me a lot of searching time. 👍


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings