Convert text to image data type - sql-server-2008-r2

I have a table that for some reason stores text as IMAGE. I can grab the data and read it using
SELECT CONVERT(NVARCHAR(MAX), CONVERT(VARBINARY(MAX), column,2)) FROM table
Now I need to insert data back in to the table. I've tried
SELECT CONVERT(IMAGE, CAST('TEST TEXT' AS VARBINARY(MAX)))
But when I test converting it back using
SELECT CONVERT(NVARCHAR(MAX), CONVERT(VARBINARY(MAX), CONVERT(IMAGE, CAST('TEST TEXT' AS VARBINARY(MAX))),2))
It returns 䕔呓吠塅 which is obviously not right as it should return "TEST TEXT"
What am I doing wrong here?

The text you're trying to store is encoded as binary ASCII characters. You're trying to convert it back into a Unicode text string, which isn't what it originally was, therefore you're getting back garbled text.
Change your source text string into a Unicode string by adding N in front of it:
SELECT CONVERT(NVARCHAR(MAX), CONVERT(VARBINARY(MAX), CONVERT(IMAGE, CAST(N'TEST TEXT' AS VARBINARY(MAX))),2))
It should return the correct text. Tested this on SQL Server 2008

You can use this one:
SELECT CONVERT(**VARCHAR(MAX)**, CONVERT(VARBINARY(MAX), **CAST('TEST TEXT' AS IMAGE)**,**0**))
Basically, you were not consistent with your character type conversions. In some parts you used NVarChar and some parts Varchar. Also, the number 2 at the end is affecting the result. In you Convert statements, when you don't specify the code, default value (0) is used. So if you are converting it back, you should use the same code.

Related

Base64 SQL Default Value, Error Validating

I'm trying to insert a base64 image code as the default value for one of my columns. I've tried encapsulating my base64 with single quotes but in SSMS it doesn't show it as a string and am getting errors when trying to save the table.
-Error validating the default for column 'mycol'
-Error modyfing column properties, Unclosed quotation mark after the character string : then shows the entire base64 code.
I also tried the following way: (N'(base64here)') but that also throws the same error.
I did a search/replace in notepad for any other single quotes in my string but there are none.
Not sure whats wrong here? Could it be that the string is too long for an varchar(MAX) field? It's 223210 characters long. I'm using SSMS gui not TSQL to enter the defaul value.
(N'(data:image/jpeg;base64,/9j/4QA2RXhpZgAASUkqAAgAAAABAJiCAgARAAAAGgAAAAAAAABTZWFuIFBhdm9uZSAyMDE5AAAAAP/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9TV1W1\=)')
The base64 string above is truncated because stackoverflow has character limit btw.
I was able to add it using TSQL
ALTER TABLE MYTBL
ADD CONSTRAINT MYCOL
DEFAULT 'base64here' FOR MYCOL;

Importing text file to database - unwanted space characters

I have a problem with importing data from a text file (comma-delimited and " text qualifier). That's the only type of export we can do from an almost-30-year-old system.
The problem comes from someone in the old system using a "space" in fields, where during import SQL shows me there is something and display cell as "NULL" When you open this text file in Excel it shows empty cell (which is correct) but cell behave differently compared to real empty cells.
Example (that's from Notepad++):
-> Orange Arrows shows TAB (did line them up to be readable)
. orange dots shows spaces
Some Column1 data has extra spaces ("N " and "B " rows) but don't cause a problem.
Column2 - first 8 columns are good. "" nothing between text qualifiers.
Rows 9-13 have space between TEXT Qualifiers. When loaded to Excel cell is empty and looks good. When loading up to SQL Server it has got errors, if I load this from Excel file SQL shows NULL in those cells. Tried to "wash" this with Access, load up good, save dbo and load up this dbo in SQL shows NULL.
Column3 same as Column2: row 1 is good, problem in row 2 and 3, then 4-8 is good showing X, and 9 till 13 shows NULL.
Any ideas how to load this up into SQL Server? Change some settings on column what data inside (to ignore the space)...?
Assuming you want spaces to be converted to empty strings in the database after importing the data, you could run SQL like
UPDATE [yourTableName]
SET [columnName] = ''
WHERE [columnName] = ' ';
Copy-and-paste for however many columns need to be sanitised and put in the correct table name and column names.
If you wanted to remove spaces from the start and end of strings at the same time as changing spaces to empty strings, you could use
UPDATE [yourTableName]
SET [columnName] = LTRIM(RTRIM([columnName]))
which would tidy up the "B " and "N " entries too.

Postgres - Full Text Search to accept emojis

I want to create a Full Text Search that accepts emojis on the query, or another type of index to search on text. For example, I have this text: Playa 🌊🌞🌴 #CobolIquique h' and PostgreSQL parse it weirdly on the emojis.
Debugging, Using SELECT * FROM ts_debug('english','Playa 🌊🌞🌴 #CobolIquique h'); I have the following result:
And I don't know why the token is considered an space symbol. If I debug the parser SELECT * FROM ts_parse('default', 'Playa 🌊🌞🌴 #CobolIquique h'); I just get the same tokens and with the tokens types ts_token_type('default') there is not a emoji type (or something similar). So, How can I create a parser to split the string correctly with the spaces and doesn't consider emojis as blank spaces? or How can I create a text index that can use emojis on the queries?
To create a new parser, which is different from default one, you should be a C programmer and you should write your own PostgreSQL extension. This extension should define the following functions:
start_function();
gettoken_function();
end_function();
lextypes_function();
headline_function(); // optional
As an example you can examine pg_tsparser module.

Postgresql plperlu and encodings

I want to generate a PDF in plperlu, store it in the database and then add it to an email as an attachment.
I am using PDF::Report to generate the PDF. The code looks like this:-
CREATE OR REPLACE FUNCTION workflow.make_pdf(report_template json, report_json json)
RETURNS bytea AS
$BODY$
use strict;
use PDF::Report;
my $pdf = new PDF::ReportNG(PageSize => 'A4', PageOrientation => "Landscape");
...
lots of tricky stuff to make PDF
...
return $pdf->Finish();
$BODY$ LANGUAGE plperlu;
This errors with invalid input syntax for type bytea which I assume is something to do with the encoding of the PDF document created.
The document itself is fine as $pdf->saveAs('/tmp/test.pdf'); creates a document that is perfectly readable.
I tried base64 encoding the result before returning it as the attachment to email will need to be in base64.
return MIME::base64::encode($pdf->Finish());
This removed the error, I can then store it in a table with:-
INSERT INTO weekly_report_pdfs(report)
VALUES (make_pdf(report_template,report_json));
Which also works fine, and can be attached to an email, but the corruption problem persists after the base64 decode.
Exporting the file directly from the database and running base64 -d test.b64 gives an invalid input error after only one line.
This appears to be something to do with the way postgres chucks bytea, as the file looks like this:-
MDA0OTY1MSAwMDAwMCBuIAowMDAwMDQ5ODU0IDAwMDAwIG4gCjAwMDAwNTAxNjcgMDAwMDAgbiAK\\012dHJhaWxlcgo8PCAvUm9vdCAxIDAgUiAvU2l6ZSA0MCAvSW5mbyA0IDAgUiA+PgpzdGFydHhyZWYK\\012
With lots of \012 separators.
Any ideas, I've been at this for hours and am completely stumped.
Returning binary from plperl
When declared as returning bytea, a pl/perl function must actually return a postgresql text representation for bytea.
Consider this excerpt from PL/Perl Functions and Arguments in the doc (and more specifically the last sentence):
Anything in a function argument that is not a reference is a string,
which is in the standard PostgreSQL external text representation for
the relevant data type. In the case of ordinary numeric or text types,
Perl will just do the right thing and the programmer will normally not
have to worry about it. However, in other cases the argument will need
to be converted into a form that is more usable in Perl. For example,
the decode_bytea function can be used to convert an argument of type
bytea into unescaped binary.
Similarly, values passed back to PostgreSQL must be in the external
text representation format. For example, the encode_bytea function can
be used to escape binary data for a return value of type bytea.
According to this, you should do:
return encode_bytea($pdf->Finish());
and then the invalid input syntax for type bytea error would go away.
Returning base64
If returning base64, the function should normally be declared as RETURNS text and the report column should be text too. That would solve the problem of having these \012 sequences that look like a line feed (ASCII code=12 in octal) expressed in a bytea string literal with postgres escape format.
The line feeds are typically added by base64 encoders every 76 characters to avoid long lines in a MIME body (RFC-4648).
If the report column stays in bytea and the function produces base64 with RETURNS text, an implicit cast should happen on INSERT and it would probably be fine. Otherwise the conversion could be made explicitly with convert_to(bytea_plperl_func(), 'US-ASCII')
But storing base64 in a bytea column doesn't make much sense.

zip code + 4 mail merge treated like an arithmetic expression

I'm trying to do a simple mail merge in Word 2010 but when I insert an excel field that's supposed to represent a zip code from Connecticut (ie. 06880) I am having 2 problems:
the leading zero gets suppressed such as 06880 becoming 6880 instead. I know that I can at least toggle field code to make it so it works as {MERGEFIELD ZipCode # 00000} and that at least works.
but here's the real problem I can't seem to figure out:
A zip+4 field such as 06470-5530 gets treated like an arithmetic expression. 6470 - 5530 = 940 so by using above formula instead it becomes 00940 which is wrong.
Perhaps is there something in my excel spreadsheet or an option in Word that I need to set to make this properly work? Please advise, thanks.
See macropod's post in this conversation
As long as the ZIP codes are reaching Word (with or without "-" signs in the 5+4 format ZIPs, his field code should sort things out. However, if you are mixing text and numeric formats in your Excel column, there is a danger that the OLE DB provider or ODBC driver - if that is what you are using to get the data - will treat the column as numeric and return all the text values as 0.
Yes, Word sometimes treats text strings as numeric expressions as you have noticed. It will do that when you try to apply a numeric format, or when you try to do a calculation in an { = } field, when you sum table cell contents in an { = } field, or when Word decides to do a numeric comparison in (say) an { IF } field - in the latter case you can get Word to treat the expression as a string by surrounding the comparands by double-quotes.
in Excel, to force the string data type when entering data that looks like a number, a date, a fraction etc. but is not numeric (zip, phone number, etc.) simply type an apostrophe before the data.
=06470 will be interpreted as a the number 6470 but ='06470 will be the string "06470"
The simplest fix I've found is to save the Excel file as CSV. Word takes it all at face value then.