I am working in SQL Server 2019 and have a table [TBL_BlobData] that, to my knowledge, contains compressed binary versions of rich text from the front end application. The schema is below:
[TBL_BlobData]
Field
Type
Example
HostFieldName
VARCHAR(255)
DocData
Size
INT
1603
Type
VARCHAR(255)
HTM;LZ77
Data
IMAGE
0x1F8B080000000000000BA557DB72DB36107DEF4CFF61EB3EB49D716C59 ...
What I would like to do, preferably all in SQL (but other methods are appreciated if this is not possible in SQL alone), is convert this data into the raw text.
I have tried converting the IMAGE to VARCHAR than DECOMPRESSING, but no luck:
SELECT DECOMPRESS(cast(cast([data] as varbinary(max)) as varchar(max)))
FROM [dbo].[VW_BlobData]
Any help is appreciated.
Related
I'm working on ssis package which exports data from SQL Server to Excel. I had a problem converting non-unicode to unicode string data types. So I created a derived Column task and converted to Unicode string [DT_WSTR] 4 columns which have a type Varchar(40) in SQL Server table. It worked with these columns. But I also have a Description column of type varchar(max) and I tried to convert it to Unicode text stream [DT_NTEXT]. It did not work.
If your source is SQL Server (as you said), you can convert it directly in your SQL Query
SELECT
CONVERT(NVARCHAR(40), 'att1')
,CONVERT(NTEXT, 'att2')
Convert your VARCHAR into NVARCHAR
Convert your TEXT into NTEXT
it's faster.
P.S. To test it (Do not forget to delete or reset your previous OLE DB Input component) -> It will be forced to reevaluate your datatype
Does it help you?
The only thing that worked was to cast a Description column in Stored Procedure as varchar(1000). I checked the max length of this field and it was about 300 characters. So I made it varchar(1000) and in Derived column Unicode string [DT_WSTR]. This was a workaround, but I still want to know how to make it in ssis package without converting data type in Stored Procedure.
I'm currently designing a MS SQL table for use in a project. The problem I'm currently facing is that the data types of "Transact SQL " are pointing to the deprecation of the types "text" and "ntext", which I always used for big texts.
Char, nchar, varchar and nvarchar may only be 8000 bytes big, this just isn't enough for a user input text, e. g. if he's writing a big article.
Is there any alternative to the obsolete data type text/ntext?
Using nvarchar(MAX) will allow you to store up to 2 GB of data. The same goes for varchar(MAX) too.
varchar(MAX) and nvarchar(MAX). Try 'em; you'll like 'em.
If you finished reading the paragraph on the MSDN page you linked, which explained that they were being removed, you would have found your answer:
ntext , text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.
If you're using SQL 2005+, you could use n / varchar(max) instead. nvarchar will use unicode, and varchar will use ascii. If this is used to store blog text, then I would go with nvarchar(max)
I'm a rookie when it comes to XML data.
I currently have a database with xml data being stored in a ntext column and this database appears to be taking up too much space. I'm trying to improve the way the data is stored, to reduce the overall size of the DB.
The way I see it, I have two options:
nvarchar(max)
xml
I will need to test the two options above by importing some data into those columns.
The problem I am having is, the XML data in the ntext column is currently stored as utf-8. In order for me to import it into the XML data type column, I will need to CAST/CONVERT the data to UTF-16?
Is this right?
Storing you data as datatype XML has two major benefits:
since it's stored as a native XML, you can do things like XQuery on top of it
since it's stored as XML it's stored in an optimized (tokenized) way, taking up less space than what the equivalent nvarchar(max) column would use up.
To convert your existing NTEXT column: just do a CAST on it.
What results do you get frmo this:
SELECT
id, ntextColumn, CAST(ntextColumn AS XML)
FROM
dbo.YourTable
I am almost sure this will work - just like that. SQL Server doesn't support UTF-8 - so your data even in the ntext column is most likely not really stored as UTF-8 (it's already been converted to SQL Server's Unicode - UCS-2/UTF-16) so I don't see any issue with converting this to datatype XML, really.
I have a table which contains an attribute called custom_fields that stores a well-formed xml:
<Root>
...
<TotalMontoSoles></TotalMontoSoles>
...
</Root>
But this attribute is not stored as a xml data type, instead it's stored as a text. What I need to do is set the TotalMontoSoles value and I was trying to accomplish that by using the modify method from XML-DML but I keep getting a
Error SQL: Explicit conversion from data type xml to text is not allowed.
error when I try to cast the column into a xml type:
DECLARE #custom_fields xml
SET #custom_fields = (SELECT CAST(custom_fields as XML) FROM UPLOAD_HEADER_TEMPORAL
#custom_fields.modify('...')
What am I doing wrong? Is there any other way I could accomplish this?
UPDATE:
Maybe it's important to point out that what I'm trying to do here is to create a procedure and I'm getting this error during compilation time.
Text column data types cannot be converted (cast) to XML. You can (should) use one of the varchar types though. Microsoft will be removing the text (and image) data type at some point in the future.
http://msdn.microsoft.com/en-us/library/ms187993%28v=sql.90%29.aspx
ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.
I have a script that successfully encrypts a credit card. I need it to work as a trigger, so that any insert done to the creditcard column automatically encrypts it. Right now, my trigger works BUT the creditcard column is a varchar. When an app tries to insert, I do this:
DECLARE #encryptedCreditCardNumber varbinary(max)
SET #encryptedCreditCardNumber = EncryptByKey(Key_GUID('testKey'), #CreditCardNumber));
That works fine, but how do I then convert the varbinary(max) to a varchar (which is what the credit card column is). The creditcard column has been a varchar for a long time at my company and a lot of legacy code depends on it being a varchar.
Thank you
Your simplest approach is going to be to convert the binary to base64 and store that in the varchar column. Base64 is a method for rendering binary data using ascii encoding so that it could be represented in formats such as XML. You can perform the conversion by doing:
select cast(N'' as xml).value('xs:base64Binary(xs:hexBinary(sql:variable("#encryptedCreditCardNumber")))', 'varchar(max)');
This goes through an XML intermediary to correctly encode the varbinary to a varchar. To reverse the process on the DB use:
select cast(N'' as xml).value('xs:base64Binary(sql:variable("#base64stringvariable"))', 'varbinary(20)');
Edit: Useful reference - http://blogs.msdn.com/sqltips/archive/2008/06/30/converting-from-base64-to-varbinary-and-vice-versa.aspx