nvarchar(max), varchar(max), or should I really decode the string and put it in an image or blob or something?
I've got a .Net SOAP web service, which saves records in a SQL 2008 DB. The service is going to be extended to accept an image, which (for better or worse) also needs to go into the DB temporarily.
To keep things simple, the service takes the image as a Base 64 encoded string, and will have to give it back as a base64 encoded string later (a different method on the same service).
I was originally just going to use nvarchar(max), and I'm sure this would work. But then I thought that base64 encoded means it could use varchar(max) instead, and use less storage space. Is this right? Or should I bite the bullet and decode the text to binary, store it as a blob, and then re-encode it on the way out again?
Finally - load storage and performance are unlikely to cause problems, this is a pet project and will be low load.
Edit: In response to @howiecamp's question, I wrote up how I was URL encoding the image here: http://www.flowerchild.org.uk/archive/2010/06/13/base-64-encoding-an-image-to-pass-across-a-web.html
Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. By consisting only of ASCII characters, base64 strings are generally url-safe, and that's why they can be used to encode data in Data URLs.
BLOBs and Base64 EncodingBLOBs are supported and stored in the XM_TYPE_STRING hash data dictionary format. The reason is that BLOBs are treated in the same manner as strings because they have already been encoded by using base64 encoding before storage into a dictionary file.
value( 'xs:base64Binary(xs:hexBinary(sql:column("bin")))' , 'VARCHAR(MAX)' ) Base64Encoding FROM ( SELECT CAST('TestData' AS VARBINARY(MAX)) AS bin ) AS bin_sql_server_temp; -- Decode the Base64-encoded string "VGVzdERhdGE=" to get back "TestData" SELECT CAST( CAST(N'' AS XML).
Base64 is a binary to text encoding scheme that is generally used to transfer content-based messages over the Internet. It works by dividing every three bits of binary data into six bit units. The newly created data is represented in a 64-radix numeral system and as seven-bit ASCII text.
Well, Base64 is a ASCII encoding, really - so definitely no need for NVARCHAR - and since it's text, I'd suggest VARCHAR(MAX)
It's pure text, up to 2 GB (should be enough), and it's a string-type, so you can use all string functions on it. NVARCHAR does indeed use twice as much storage - always 2 bytes per character - and is totally unnecessary in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With