How to insert binary file into a binary SQL column? - sql-server

I have a SQL Server 2008 database with a table containing an image column.
Any idea how to write query to insert bin file to the image field?
Thank you in advance!

Totally agree with marc_s.
But your query will be something like this.
INSERT yourtable(ImageColumn)
SELECT image FROM
OPENROWSET(BULK, 'filename.png', SINGLE_BLOB) AS blobtable(image)

Related

Error in Insert to varbinary(max) String or binary data would be truncated. The statement has been terminated

I have table with definition:
I have image files near 80kb. When I am trying insert data to table Usluga like this:
INSERT [dbo].[Usluga] (Nazvanie, Cena_za_poseshenie, Image)
SELECT N'Персональный тренинг', 50, ThumbnailPhoto.*
FROM OPENROWSET
(BULK 'MyFilePathToImage.jpg', SINGLE_BLOB) ThumbnailPhoto
go
INSERT [dbo].[Usluga] (Nazvanie, Cena_za_poseshenie, Image)
SELECT N'Бокс', 90, ThumbnailPhoto.*
FROM OPENROWSET
(BULK 'MyFilePathToImage.jpg', SINGLE_BLOB) ThumbnailPhoto
go
I give error
String or binary data would be truncated.
The statement has been terminated.
But varbinary(max) allows save data from 0 through 2^31-1 (2,147,483,647) bytes.
How do I fix this?
Perhaps you're looking at the wrong column. Try resizing your nvarchar(20) column so it can accept more than 20 characters.
I was getting similar error when trying to insert a image file as a blob to
SQL Server database, it would throw "RIGHT TRUNCATION" error.
I solved my problem by changing the blob column data type from VARBINARY(MAX) to
IMAGE. The varbinary(max) datatype was supposed to replace the image datatype in newer SQL Server instances, my instance was SQL Server 2012. If using the Image data type works for you, then just keep in mind that the max size for this type is 2 GB.

Inserting A mdi file in SQL Server

I am trying to insert a mdi file in a SQL Server 2008 table.
Do you know how can I do this?
My table has the following structure
Create table employées
(
Id_employee int,
Mdi_employee image
)
Convert your MDI file into Byte format and then use normal query to insert that image.
For More information : C# Resize jpg image, convert to byte and save into database using varbinary

Mysterious:Selecting a large Xml in Sql Server

I am having a column in my table which stores XML data as a varchar(MAX).For example one of my value has around 1 LAC characters.While selecting it from the Table, i end up getting only 43679 characters for all samples.
What is the reason behind this mystery?Please,if there is any way to retrieve the complete data,help.
Try using settings of sql server management studio.
try to select with a cast:
select top 1 cast(ColumnName as xml) from Table

How to Insert korean text in ms-sql server?

I tried to insert korean text into my ms sql server table, but it seems not working properly because all characters are broken like "????" as following:
The type of column is nvarchar, and I query as follow to put the data: insert into mytable values('텍스트'). And the collation is SQL_Latin1_General_CP1_CI_AS, but I wonder if the collation is the problem.
Please help me to find solution
Try this
While working with nvarchar datatype always use N'...'
insert into mytable values(N'텍스트');
This sqlfiddle example shows the difference if you don't use N'...'.
http://sqlfiddle.com/#!6/fab9a/1

How to script VARBINARY to copy it from one DB to another using a script?

I need to generate an SQL insert script to copy data from one SQL Server to another.
So with .net, I'm reading the data a given SQL Server table and write this to a new text file which can then be executed in order to insert this data on other databases.
One of the columns is a VARBINARY(MAX).
How should and can I transform the obtained byte[] into text for the script so that it can still be inserted on the other databases?
SSMS shows this data as hex string. Is this the format to use?
I can get this same format with the following
BitConverter.ToString(<MyByteArray>).Replace("-", "")
But how can this be inserted again?
I tried
CONVERT(VARBINARY(MAX), "0xMyHexString")
This does an insert, but the value is not the same as in the source table.
It turned out you can just directly insert the hex string, no need to convert anything:
INSERT TableName (VarBinColumnName)
VALUES (0xMyHexString)
Just don't ask why I didn't test this directly...
There are two questions on SO that may help:
What is the fastest way to get varbinary data from SQL Server into a C# Byte array?
and
How Do I Insert A Byte[] Into an SQL Server VARBINARY column?

Resources