October 20, 2023 at 4:37 pm
Good afternoon, Devs. My name is André, and I'm new here.
I'd like to ask for your assistance with a problem I'm currently facing. I've taken over the migration of an application where the database is in SQL Server, and there's a table containing files stored in a column of type "ntext." According to the client, these files were compressed in zip format and saved by the previous company's system.
However, I've been unable to extract the information in any way. Below, you can see an image of the table and its contents. I had to create a column of type "varchar(max)" and copy the content from the "docArquivoAssociado" column in an attempt to solve the issue, but with no success.
Any suggestions?
October 20, 2023 at 5:36 pm
whoever stored those in next likely caused them to be corrupted without repair.
zip files should have been stored as image or varbinary(max)
assuming they were stored correctly, e.g. no bytes have been changed/lost, you would save that particular column as a stream to a file and it should then open as a zip file.
October 20, 2023 at 5:45 pm
Its more involved than that, you need to know the text encoding and the specific compression algorithm as well. You will have to know how that was written to the table to reverse it and as frederico alluded to, get it changed to use varbinary as storage ASAP, then extend your table to include the encoding and compression metadata so someone else doesn't have to reverse engineer it.
October 20, 2023 at 5:50 pm
But their system can still extract the files within their Delphi-based system. However, I need to convert this database, but I also need to extract the files. I created a column of type varchar(max) and saved the data from the ntext column, but I'm still unable to do it.
October 20, 2023 at 5:58 pm
where is the current data used by the Delphi system?
and how did you copy it into your new table (and as I said it should be varbinary(max))
and can you ask them for the code where they read from their storage/db table and store on the file - that may also give you some guidance on what to do.
October 20, 2023 at 7:26 pm
ZIP files are binary files so will not save correctly as a string. Binary can be encoded as a string although the size will obviously be increased. One way to encode binary as a string is called Base64. This can be done in SQL Server but probably should be done by the application.
https://medium.com/falafel-software/t-sql-easy-base64-encoding-and-decoding-872ad184bd7e
October 20, 2023 at 7:57 pm
ZIP files are binary files so will not save correctly as a string. Binary can be encoded as a string although the size will obviously be increased. One way to encode binary as a string is called Base64. This can be done in SQL Server but probably should be done by the application.
https://medium.com/falafel-software/t-sql-easy-base64-encoding-and-decoding-872ad184bd7e
as long as you have a varbinary capable field there is no need to store it as a string - hence conversion is not required. and on this case, and based on the image, the file was stored as binary values, but on a ntext (then moved to a varchar) field - this is the bit that is wrong.
as long as the data is correctly stored, saving it to a file is a straightforward step in the majority of languages.
October 30, 2023 at 5:51 am
In case you did not pick up the crucial bit from the previous post, data stored as ntext must get converted to nvarchar.
If you go from ntext to plain old varchar you will lose some data.
Original author: https://github.com/SQL-FineBuild/Common/wiki/ 1-click install and best practice configuration of SQL Server 2019, 2017 2016, 2014, 2012, 2008 R2, 2008 and 2005.
When I give food to the poor they call me a saint. When I ask why they are poor they call me a communist - Archbishop Hélder Câmara
October 31, 2023 at 5:15 pm
So, I tried converting to "nvarchar(max)" and "varbinary(max)," but without success. Therefore, I had to resort to a more radical approach, which involved extracting data from the client's portal. This compelled their system to generate a file for me to download and store in my database.
October 31, 2023 at 5:22 pm
you never answered the questions of how you loaded from their system onto SQL Server - neither on how you are extracting it and trying to save to a file - as well as how their own code works.
as well you never told us what ERROR you get after saving the files and trying to open them.
as for your latest comment - I hope you followed our advise and you are loading those files onto a varbinary(max) column - or (likely better for some) using filestream for it.
October 31, 2023 at 7:36 pm
So, I tried converting to "nvarchar(max)" and "varbinary(max)," but without success. Therefore, I had to resort to a more radical approach, which involved extracting data from the client's portal. This compelled their system to generate a file for me to download and store in my database.
I sincerely hope you have a backup before all those changes were made. And, unless you know exactly how to do the unzipping, you need to stop shooting in the barn... you might hit your favorite cow. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
January 31, 2024 at 7:10 pm
Good afternoon, my friend. I apologize for the delay in responding; we encountered some setbacks in the project and had to shift our focus. But to answer your question: Their system is "Desktop," and somehow, it has an integration with their portal, which, in turn, generates the URL for the file through a POST request. I read and save it in my PostgreSQL database.
I created a column in the table called "blob" and converted the content to this column without altering the value of the original column, just to try to extract it. However, even with this approach, I was unsuccessful.
-- Adicionando uma nova coluna blob à tabela
ALTER TABLE dbo.AD_ComissaoPerm_ExpedienteAtaImagem
ADD ColunaBlob VARBINARY(MAX);
-- Atualizando os dados na nova coluna com a conversão de ntext para varbinary(max)
UPDATE dbo.AD_ComissaoPerm_ExpedienteAtaImagem
SET ColunaBlob = CAST(CAST(Imagem AS NVARCHAR(MAX)) AS VARBINARY(MAX));
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply