Convert Image DataType To String in SQL Server

谁说胖子不能爱 提交于 2021-01-27 12:36:08

问题


I have a Column having Image datatype in MS-SQL SERVER 2012. I want to get it as string in SQL Query..

I have Tried This:

SELECT 
'empphoto : '+ ISNULL(CONVERT(VARCHAR(MAX), CONVERT(VARBINARY(MAX), empphoto)),'') 
from emppersonal where --some condition

--empphoto Columns is of Image datatype

output looks corrupted (Just 4 characters).

OutPut looks like:

empphoto : ÿØÿà

enter image description here

How can be an Image datatype be converted to string in MS-SQL Server?


回答1:


You can extract the image value as BASE64 by running it through for xml path().

Try:

select 'empphoto : '+(select empphoto as '*' for xml path(''))

Result will look something like this.

empphoto : /9j/4AAQSkZJRgABAQAAAQABAAD/wAARCADw

To go the other way you have to remove the first 11 characters (empphoto :), cast to XML and extract the value as varbinary(max)..

select cast(stuff(YourTextColumn, 1, 11, '') as xml).value('.', 'varbinary(max)')


来源:https://stackoverflow.com/questions/27224054/convert-image-datatype-to-string-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!