Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert image datatype into varchar in sql server 2008

Tags:

We have TestPartner database in SQL Server. The descriptions of the bugs are stored in "image" datatype column. We need to write a query to display the data as html table. We have this query that reads the data from the respective tables to display the information as xml using For XML. But converting image datatype to varchar throws an exception: "FOR XML could not serialize the data for node 'TD' because it contains a character (0x0002) which is not allowed in XML. To retrieve this data using FOR XML, convert it to binary, varbinary or image data type and use the BINARY BASE64 directive.".

Query:

DECLARE @ResultsTable nvarchar(MAX) --Create the XML table with the query results SET @ResultsTable = N'<H3>QA Automation Tests Results Summary </H3>' + N'<table border="1">' + N'<tr><th>Test Name</th><th>Execution Date</th>' + N'<th>Check Name</th><th>Description</th></tr>' + CAST ( ( select distinct Name as TD, '', (Select CAST(CONVERT(nchar(100),CAST( TPCommandDetail AS BINARY(100) )) as VARCHAR(100)) ) as TD, '' FROM TestPartnerDB.TP_RESULTS_RECORDS FOR XML PATH('tr'), TYPE  ) AS nvarchar(max) ) + N'</table>'  SELECT @ResultsTable 

Surprisingly it works for some records and as soon as you bump the size to say 200. It throws error again. I also tried:

Select CONVERT(varchar(1000), convert(varbinary(1000), tpcommanddetail)) From TestPartnerDB.TP_RESULTS_RECORDS 

This returns weird characters for each row. Does anyone know how to get this thing to work?

like image 359
Sri Reddy Avatar asked Oct 24 '11 18:10

Sri Reddy


People also ask

What datatype is used for image in SQL Server?

The IMAGE data type in SQL Server has been used to store the image files. Recently, Microsoft began suggesting using VARBINARY(MAX) instead of IMAGE for storing a large amount of data in a single column since IMAGE will be retired in a future version of MS SQL Server.

Can we store image in SQL table?

SQL Server allows storing files. In this article, we learned how to insert a single image file into a SQL Server table using T-SQL. We also learned how to copy multiple files from a folder into SQL Server in a table. Finally, we worked in SQL Server Reporting Services to view the images inserted.

How do I display an image in SQL Server?

How to view images stored in your database Start SQL Image Viewer and connect to your database. For SQL Server databases, tables containing blob columns will be highlighted in green in the list of database objects. Write the query to retrieve your images, and execute the query.

What is CAST function in SQL?

The CAST() function converts a value (of any type) into a specified datatype.


2 Answers

The simple answer is

select cast(cast(my_column as varbinary(max)) as varchar(max)) as column_name from   my_table 

This converts the column to varchar format. nvarchar(max) might be better if you have unicode data.

like image 116
kristianp Avatar answered Oct 19 '22 04:10

kristianp


You can convert also like this

convert (varchar(max) , convert (varbinary (max) , blob_data)), cast(cast(blob_data as binary) as varchar(max))  
like image 21
NIrav Modi Avatar answered Oct 19 '22 05:10

NIrav Modi