Retrieve original filename of varbinary image file - sql-server

I've stored an image in varbinary type in SQL Server.
Here's an example of data:
0x89504E470D0A1A0A0000000D49484452000005E7000002DB0802000000FFE6ACCA00000001735247420 (and much more characters after)
Originally, it's filename is : test.jpg
I'd like to retrieve "test.jpg" by a stored procedure in SQL Server instead of this long and terrifying string. Is it possible?

No. A file's name is typically only stored in the filesystem; not in the contents of the file. So unless you stored the filename in a separate column, you can't retrieve it with TSQL.

Related

How to get file type from binary data in SQL Server File table?

I want to get file type from binary data in a column File_Stream in FileTable.
In FileTable, there is File_Type column but it's not reliable.
Pay attention to the following queries:
These files are the same!
The file_type column value is derived from the file name, not the binary contents of the file. SQL Server has no notion of what the actual binary contents represent.

I have a problem inserting more than 255 chars per column into an Excel file using INSERT INTO OPENROWSET from SQL Server

I am getting an error while exporting data from SQL Server to an already created .xlsx file using openrowset.
It works fine most of times, but when the data comes in of the field as a large string, while inserting into Excel, it shows this error:
The statement has been terminated, string or binary data would be truncated.
Data gets inserted into table, but while inserting in Excel, this error appears. Please help me find a solution.
As the error mentions "data would be truncated", you should be provide a longer string value into a placeholder or field that has a smaller storage size.
For example, the source field may have data type nvarchar(max) and in your SQL development or where a mapping exists, you assing the values into a smaller data size type. For example, in source table you have a string value 5000 characters, but during the process it is assigned to a nvarchar(4000) then a data truncation will occur
I would suggest you to check data mappings in your statements
Regedit: Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\14.0\Access Connectivity Engine\Engines\Excel
In Regedit, set the "TypeGuessRows" value on this path to a value greater than 8, for example 100000.enter image description here

Using SSIS variable (#[User::FileName]) as part of UPDATE in `Execute Sql Server Task'?

We have an SSIS project that reads from a text file and inserts to a sql server table.
The Flat File Connection Manager for the Flat File Source uses a variable value as ConnectionString property. So essentially, it's expression ConnectionString = #[User::FileName]. This is working fine, and it's reading the file from the variable into the table.
Since the filename needs to be saved into the table, we need to also insert the filename into the table that's already storing the contents of the actual file. Currently, each line in our text file has 5 comma-separated value that we read into table [TableFile], which also has 5 columns.
The change would be that [TableFile] will now have an additional column [FileName]. Therefore, the Data Flow Task that runs and inserts the contents of the file will also insert the filename (already saved in variable #[User::FileName]).
Since the table will always have one filename, I was thinking of somehow using an Execute Sql Server Task item to update the table with this value. But I have no idea of how to include the value of #[User::FileName] in the SQL UPDATE statement.
Thanks.
Create another SSIS variable to build and hold your entire UPDATE sql string, using the FileName variable to build that part of the string.
Then in the Execute SQL task, set SQL Source Type to "variable", and choose your SQL String variable as the Source Variable property.

search string in varbinary column of SQL Server table

I have a table which has 3 columns:
ProblemID
FileContent
FileName
I want to search for a user specified string from the fileContent column. Is it possible to convert binary data in VARCHAR? I tried it but it gives results in different format.
i am storing .pdf files only
You can't search real binary data in SQL Server if the binary format is Word or Excel or anything except a raw text file

Passing huge amounts of data as an hexadecimal (0x123AB...) parameter of a clr stored procedure in sql server

I post this question has followup of This question, since the thread is not recieving more answers.
I'm trying to understand if it is possible to pass as a parameter of a CLR stored procedure a large amount of data as "0x5352532F...".
This is to avoid to send the data directly to the CLR stored procedure, instead of sending ti to a temporary DB field and from there passing it as varbinary(max) parmeter to the CLR stored procedure.
I have a triple question:
1) is it possible, if yes how? Let's say i want to pass a pdf file to the CLR stored procedure (not the path, the full bits that make up the file). Something like:
exec MyCLRStoredProcs.dbo.insertfile
#file_remote_path ='c:\temp\test_file.txt' ,
#file_contents=0x4D5A90000300000004000.... --(this long list is the file content)
where insertfile is a stored proc that writes to the server path (at file_remote_path) the binary data I pass as (file_contents).
2) is it there corruption risk of adopting this approach (or it is the same approach that sql server uses behind the scenes)?
3) how to convert the content of a file into the "0x23423..." hexadecimal representation
What is your goal? Are you trying to transfer a file from the client filesystem to the server filesystem? If so, you might want to look at a web service file transfer mechanism.
Do you want to persist the data into the database? If so, and you have access to SQL Server 2008, I recommend looking at the new FILESTREAM type. This type maintains the link between the database and the file system for you.
Alternatively, if you don't have SQL Server 2008, you get to choose between saving it as a file and maintaining a string path to it in the database or storing the contents of a file in a VARBINARY(MAX) column.
If all you want is to get the data into the database, you don't need a CLR proc. You can save it directly to the database, or you can code a SQL stored proc to do so.
Assuming you keep the approach of sending this to a CLR proc:
1) is it possible, if yes how?
Sure, why not. The code you wrote looks like a good example. The stored proc will need to convert the string into bytes.
2) is it there corruption risk of adopting this approach
I'm not sure what you mean here. Will SQL Server randomly replace characters in your string? No. Might you accidentally hit some sort of limit? Yes, possibly; the maximum size of NVARCHAR(MAX) is 2^31-1, or 2,147,483,647 characters. But I doubt you'd have a PDF that size. Might you lose the link between the file on disk and the database path to it? Yes, though FILESTREAM should take care of that for you.
3) how to convert the content of a file into the "0x23423..." hexadecimal representation
There are many examples on the Internet on how to do this. Here's one:
How do you convert Byte Array to Hexadecimal String, and vice versa?

Resources