SQL Server: How to convert image data type to plain text and back?
Following is a demonstration of one of the ways of using the SQL Convert function to convert image data type to plain text and back. FYI: Image data type is used to store binary data and can store variable-length binary data from 0 through 2^31-1 (2,147,483,647) bytes .
Microsoft advocates to avoid the further using of text, ntext and image data types since these data types will be removed from future versions of SQL Server. Further reading can be done about at Using Large-Value Data Types .
–The following example shows how plain text
–can be converted to image type data
SELECT CONVERT(binary(8), ‘Caveman’) AS ‘text to binary’
OUTPUT: text to binary
–The following example shows how image type data
–can be converted to Text
SELECT CONVERT(char(8), 0x436176656D616E00) AS ‘binary to text’
OUTPUT: binary to text
Tip: CAST vs CONVERT function
Both these functions can be used for the explicit conversion of an expression of a datatype to another . CONVERT function is more powerful of the two functions in give us more flexibility during conversion.
CAST ( expression AS data_type )
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )